You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by klaasjan elzinga <kl...@gmail.com> on 2007/09/06 22:40:13 UTC

DDL for foreign key constraints.

Hi there,

I am trying to get openjpa to generate foreign key constraints. I have
to following annotations:

... on the parent:

    @OneToMany(targetEntity=Message.class, cascade=CascadeType.ALL,
mappedBy="mailingList")
    private List<Message> messages = new ArrayList<Message>();


... on the child

    @ManyToOne(targetEntity=MailingList.class, optional=false)
    @JoinColumn(referencedColumnName="id", table="mailinglist")
    private MailingList mailingList;

But openjpa w/ mysql does not generate a foreign key constraint:
mysql> show create table message;
| message | CREATE TABLE `message` (
  `id` bigint(20) NOT NULL,
  `state` smallint(6) default NULL,
  `version` int(11) default NULL,
  `mailingList_id` bigint(20) default NULL,
  PRIMARY KEY  (`id`),
  KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

Am I missing something? (besides the constraint ;)

Regards,

KlaasJan

Re: DDL for foreign key constraints.

Posted by Patrick Linskey <pl...@gmail.com>.
> Of course, as you said, this locks you to OpenJPA.

Note that the lock-in for this is just that you must have the OpenJPA
jars available at compile time (and maybe runtime, depending on your
JVM version). I assume that other products also have ways of
configuring foreign keys on a per-relation basis.

-Patrick

On 9/10/07, CASERO Jaime <JC...@covansys.com> wrote:
> The parameter you are setting is the default for all foreign keys. This
> means that "restrict" will be the action for all you foreign keys. In my
> case, at least, this is not the desired behaviour. Sometimes I need
> "restrict", and sometimes "cacacade" or "set null". Patrick, was the one
> suggesting me "ForeignKey" annotation. Of course, as you said, this
> locks you to OpenJPA.
>
> If all you FK have the same action, then this property should do the
> job.
>
> Look at the mail I sent
> http://mail-archives.apache.org/mod_mbox/openjpa-users/200708.mbox/%3cF2
> E9537648B0EB428674C3B7B6D8834D05C0E906@fth-ex04.CVNS.corp.covansys.com%3
> e
>
> Try to override the action of your FK, using "OneToMany" or "ManyToOne"
> annotations, with a different one than the "ForeignKeyDeleteAction"
> property. I got always the same DDL for FKs -> setting actions to the
> one set for the property.
>
>
> -----Original Message-----
> From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> Sent: Friday, September 07, 2007 11:27 PM
> To: users@openjpa.apache.org
> Subject: Re: DDL for foreign key constraints.
>
> That works, I didnt even have to use the @ForeignKey annotation as
> suggested earlier.
>
> KJ
>
> On 9/7/07, Prashant Bhat <pr...@gmail.com> wrote:
> > Not sure about your exact use case, but the following configuration
> works
> > for me (both with MappingTool to generate schema and at runtime with
> the
> > default operationOrderUpdateManager)
> >
> > -MappingDefaults
> >
> jpa(ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict)
> >
> > Hope this helps,
> > Prashant
> >
> > On 9/7/07, klaasjan elzinga <kl...@gmail.com> wrote:
> > >
> > > Doesnt that tie me to the openjpa implementation? Is there a reason
> > > why a foreign key constraint should not be created if the database
> > > makes it possible, ie mysql / oracle etc.? Or is it specified by the
> > > jpa specs that no foreign keys are to be made?
> > >
> > > Regards,
> > >
> > > KlaasJan
> > >
> > > On 9/7/07, CASERO Jaime < JCasero@covansys.com> wrote:
> > > > I'm afraid you have to use "@ForeignKey" annotation to force JPA
> to
> > > > create the foreign key at DB level.
> > > >
> > > >
> http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
> > > > ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk
> > > >
> > > > Use this setting in your persistence.xml during development, to
> set JPA
> > > > to create the necessary Foreign Key, even if the schema was
> already
> > > > created.
> > > >             <property name="openjpa.jdbc.SynchronizeMappings"
> > > >                 value="buildSchema(ForeignKeys=true)"/>
> > > >
> > > > -----Original Message-----
> > > > From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> > > > Sent: Thursday, September 06, 2007 10:40 PM
> > > > To: users@openjpa.apache.org
> > > > Subject: DDL for foreign key constraints.
> > > >
> > > > Hi there,
> > > >
> > > > I am trying to get openjpa to generate foreign key constraints. I
> have
> > > > to following annotations:
> > > >
> > > > ... on the parent:
> > > >
> > > >     @OneToMany(targetEntity=Message.class,
> cascade=CascadeType.ALL,
> > > > mappedBy="mailingList")
> > > >     private List<Message> messages = new ArrayList<Message>();
> > > >
> > > >
> > > > ... on the child
> > > >
> > > >     @ManyToOne(targetEntity=MailingList.class, optional=false)
> > > >     @JoinColumn(referencedColumnName="id", table="mailinglist")
> > > >     private MailingList mailingList;
> > > >
> > > > But openjpa w/ mysql does not generate a foreign key constraint:
> > > > mysql> show create table message;
> > > > | message | CREATE TABLE `message` (
> > > >   `id` bigint(20) NOT NULL,
> > > >   `state` smallint(6) default NULL,
> > > >   `version` int(11) default NULL,
> > > >   `mailingList_id` bigint(20) default NULL,
> > > >   PRIMARY KEY  (`id`),
> > > >   KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
> > > > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
> > > >
> > > > Am I missing something? (besides the constraint ;)
> > > >
> > > > Regards,
> > > >
> > > > KlaasJan
> > > >
> > > > Confidentiality Statement:
> > > >
> > > > This message is intended only for the individual or entity to
> which it
> > > is addressed. It may contain privileged, confidential information
> which is
> > > exempt from disclosure under applicable laws. If you are not the
> intended
> > > recipient, please note that you are strictly prohibited from
> disseminating
> > > or distributing this information (other than to the intended
> recipient) or
> > > copying this information. If you have received this communication in
> error,
> > > please notify us immediately by return email.
> > > > -----------------------------
> > > >
> > >
> >
>
> Confidentiality Statement:
>
> This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by return email.
> -----------------------------
>


-- 
Patrick Linskey
202 669 5907

Re: DDL for foreign key constraints.

Posted by Patrick Linskey <pl...@gmail.com>.
> - If you persist something with a reference that doesnt exists it will
> complain about an unmanaged entity. So anything persisted is ok(?).

That's just because you didn't set cascade-persist for your relation,
or set the cascade-persist orm.xml setting to true.

Sadly, the JPA 1.0 spec is set up so that by default, you must call
'EM.persist()' on related elements. If you set the cascade attribute
of the relation annotation to 'PERSIST' or 'ALL', OpenJPA will
automatically make the persist call for you (which is, to the best of
my knowledge, what you want to do 100% of the time). Alternately, you
can toggle the default for all your relations in a persistence unit by
using the persistence-unit-defaults cascade-persist element in the
orm.xml document.

> them. I would have expected a foreign key to be generated for every
> relation (OneToMany etc.).

In OpenJPA, the default behavior for what to do when generating a
schema is configurable. Currently, schema generation is outside the
scope of the JPA spec, so different vendors do this differently.

-Patrick

On 9/10/07, klaasjan elzinga <kl...@gmail.com> wrote:
> Maybe the foreign key constraints are of no importance to jpa. I am
> led to this conclusion based on the following:
> - If you persist something with a reference that doesnt exists it will
> complain about an unmanaged entity. So anything persisted is ok(?).
> - And if, for example, a 2nd level cache is put between open jpa and
> the database, what is the meaning of the constraint. The user who
> inserted the rows is probably long gone home when the cache expires
> and the objects are evicted. The openjpa implementation should have
> checked it already.
> - And I hope that the defaults are 'sane' defaults, that is do not
> generate any foreign key constraints, even if the database supports
> them. I would have expected a foreign key to be generated for every
> relation (OneToMany etc.).
>
> But foreign keys are a part of every day life, at least where I live.
> So the I just use the restrict for all option.
>
> Thx for the help,
>
> KlaasJan
>
> On 9/10/07, CASERO Jaime <JC...@covansys.com> wrote:
> > The parameter you are setting is the default for all foreign keys. This
> > means that "restrict" will be the action for all you foreign keys. In my
> > case, at least, this is not the desired behaviour. Sometimes I need
> > "restrict", and sometimes "cacacade" or "set null". Patrick, was the one
> > suggesting me "ForeignKey" annotation. Of course, as you said, this
> > locks you to OpenJPA.
> >
> > If all you FK have the same action, then this property should do the
> > job.
> >
> > Look at the mail I sent
> > http://mail-archives.apache.org/mod_mbox/openjpa-users/200708.mbox/%3cF2
> > E9537648B0EB428674C3B7B6D8834D05C0E906@fth-ex04.CVNS.corp.covansys.com%3
> > e
> >
> > Try to override the action of your FK, using "OneToMany" or "ManyToOne"
> > annotations, with a different one than the "ForeignKeyDeleteAction"
> > property. I got always the same DDL for FKs -> setting actions to the
> > one set for the property.
> >
> >
> > -----Original Message-----
> > From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> > Sent: Friday, September 07, 2007 11:27 PM
> > To: users@openjpa.apache.org
> > Subject: Re: DDL for foreign key constraints.
> >
> > That works, I didnt even have to use the @ForeignKey annotation as
> > suggested earlier.
> >
> > KJ
> >
> > On 9/7/07, Prashant Bhat <pr...@gmail.com> wrote:
> > > Not sure about your exact use case, but the following configuration
> > works
> > > for me (both with MappingTool to generate schema and at runtime with
> > the
> > > default operationOrderUpdateManager)
> > >
> > > -MappingDefaults
> > >
> > jpa(ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict)
> > >
> > > Hope this helps,
> > > Prashant
> > >
> > > On 9/7/07, klaasjan elzinga <kl...@gmail.com> wrote:
> > > >
> > > > Doesnt that tie me to the openjpa implementation? Is there a reason
> > > > why a foreign key constraint should not be created if the database
> > > > makes it possible, ie mysql / oracle etc.? Or is it specified by the
> > > > jpa specs that no foreign keys are to be made?
> > > >
> > > > Regards,
> > > >
> > > > KlaasJan
> > > >
> > > > On 9/7/07, CASERO Jaime < JCasero@covansys.com> wrote:
> > > > > I'm afraid you have to use "@ForeignKey" annotation to force JPA
> > to
> > > > > create the foreign key at DB level.
> > > > >
> > > > >
> > http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
> > > > > ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk
> > > > >
> > > > > Use this setting in your persistence.xml during development, to
> > set JPA
> > > > > to create the necessary Foreign Key, even if the schema was
> > already
> > > > > created.
> > > > >             <property name="openjpa.jdbc.SynchronizeMappings"
> > > > >                 value="buildSchema(ForeignKeys=true)"/>
> > > > >
> > > > > -----Original Message-----
> > > > > From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> > > > > Sent: Thursday, September 06, 2007 10:40 PM
> > > > > To: users@openjpa.apache.org
> > > > > Subject: DDL for foreign key constraints.
> > > > >
> > > > > Hi there,
> > > > >
> > > > > I am trying to get openjpa to generate foreign key constraints. I
> > have
> > > > > to following annotations:
> > > > >
> > > > > ... on the parent:
> > > > >
> > > > >     @OneToMany(targetEntity=Message.class,
> > cascade=CascadeType.ALL,
> > > > > mappedBy="mailingList")
> > > > >     private List<Message> messages = new ArrayList<Message>();
> > > > >
> > > > >
> > > > > ... on the child
> > > > >
> > > > >     @ManyToOne(targetEntity=MailingList.class, optional=false)
> > > > >     @JoinColumn(referencedColumnName="id", table="mailinglist")
> > > > >     private MailingList mailingList;
> > > > >
> > > > > But openjpa w/ mysql does not generate a foreign key constraint:
> > > > > mysql> show create table message;
> > > > > | message | CREATE TABLE `message` (
> > > > >   `id` bigint(20) NOT NULL,
> > > > >   `state` smallint(6) default NULL,
> > > > >   `version` int(11) default NULL,
> > > > >   `mailingList_id` bigint(20) default NULL,
> > > > >   PRIMARY KEY  (`id`),
> > > > >   KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
> > > > > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
> > > > >
> > > > > Am I missing something? (besides the constraint ;)
> > > > >
> > > > > Regards,
> > > > >
> > > > > KlaasJan
> > > > >
> > > > > Confidentiality Statement:
> > > > >
> > > > > This message is intended only for the individual or entity to
> > which it
> > > > is addressed. It may contain privileged, confidential information
> > which is
> > > > exempt from disclosure under applicable laws. If you are not the
> > intended
> > > > recipient, please note that you are strictly prohibited from
> > disseminating
> > > > or distributing this information (other than to the intended
> > recipient) or
> > > > copying this information. If you have received this communication in
> > error,
> > > > please notify us immediately by return email.
> > > > > -----------------------------
> > > > >
> > > >
> > >
> >
> > Confidentiality Statement:
> >
> > This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by return email.
> > -----------------------------
> >
>


-- 
Patrick Linskey
202 669 5907

Re: DDL for foreign key constraints.

Posted by klaasjan elzinga <kl...@gmail.com>.
Maybe the foreign key constraints are of no importance to jpa. I am
led to this conclusion based on the following:
- If you persist something with a reference that doesnt exists it will
complain about an unmanaged entity. So anything persisted is ok(?).
- And if, for example, a 2nd level cache is put between open jpa and
the database, what is the meaning of the constraint. The user who
inserted the rows is probably long gone home when the cache expires
and the objects are evicted. The openjpa implementation should have
checked it already.
- And I hope that the defaults are 'sane' defaults, that is do not
generate any foreign key constraints, even if the database supports
them. I would have expected a foreign key to be generated for every
relation (OneToMany etc.).

But foreign keys are a part of every day life, at least where I live.
So the I just use the restrict for all option.

Thx for the help,

KlaasJan

On 9/10/07, CASERO Jaime <JC...@covansys.com> wrote:
> The parameter you are setting is the default for all foreign keys. This
> means that "restrict" will be the action for all you foreign keys. In my
> case, at least, this is not the desired behaviour. Sometimes I need
> "restrict", and sometimes "cacacade" or "set null". Patrick, was the one
> suggesting me "ForeignKey" annotation. Of course, as you said, this
> locks you to OpenJPA.
>
> If all you FK have the same action, then this property should do the
> job.
>
> Look at the mail I sent
> http://mail-archives.apache.org/mod_mbox/openjpa-users/200708.mbox/%3cF2
> E9537648B0EB428674C3B7B6D8834D05C0E906@fth-ex04.CVNS.corp.covansys.com%3
> e
>
> Try to override the action of your FK, using "OneToMany" or "ManyToOne"
> annotations, with a different one than the "ForeignKeyDeleteAction"
> property. I got always the same DDL for FKs -> setting actions to the
> one set for the property.
>
>
> -----Original Message-----
> From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> Sent: Friday, September 07, 2007 11:27 PM
> To: users@openjpa.apache.org
> Subject: Re: DDL for foreign key constraints.
>
> That works, I didnt even have to use the @ForeignKey annotation as
> suggested earlier.
>
> KJ
>
> On 9/7/07, Prashant Bhat <pr...@gmail.com> wrote:
> > Not sure about your exact use case, but the following configuration
> works
> > for me (both with MappingTool to generate schema and at runtime with
> the
> > default operationOrderUpdateManager)
> >
> > -MappingDefaults
> >
> jpa(ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict)
> >
> > Hope this helps,
> > Prashant
> >
> > On 9/7/07, klaasjan elzinga <kl...@gmail.com> wrote:
> > >
> > > Doesnt that tie me to the openjpa implementation? Is there a reason
> > > why a foreign key constraint should not be created if the database
> > > makes it possible, ie mysql / oracle etc.? Or is it specified by the
> > > jpa specs that no foreign keys are to be made?
> > >
> > > Regards,
> > >
> > > KlaasJan
> > >
> > > On 9/7/07, CASERO Jaime < JCasero@covansys.com> wrote:
> > > > I'm afraid you have to use "@ForeignKey" annotation to force JPA
> to
> > > > create the foreign key at DB level.
> > > >
> > > >
> http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
> > > > ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk
> > > >
> > > > Use this setting in your persistence.xml during development, to
> set JPA
> > > > to create the necessary Foreign Key, even if the schema was
> already
> > > > created.
> > > >             <property name="openjpa.jdbc.SynchronizeMappings"
> > > >                 value="buildSchema(ForeignKeys=true)"/>
> > > >
> > > > -----Original Message-----
> > > > From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> > > > Sent: Thursday, September 06, 2007 10:40 PM
> > > > To: users@openjpa.apache.org
> > > > Subject: DDL for foreign key constraints.
> > > >
> > > > Hi there,
> > > >
> > > > I am trying to get openjpa to generate foreign key constraints. I
> have
> > > > to following annotations:
> > > >
> > > > ... on the parent:
> > > >
> > > >     @OneToMany(targetEntity=Message.class,
> cascade=CascadeType.ALL,
> > > > mappedBy="mailingList")
> > > >     private List<Message> messages = new ArrayList<Message>();
> > > >
> > > >
> > > > ... on the child
> > > >
> > > >     @ManyToOne(targetEntity=MailingList.class, optional=false)
> > > >     @JoinColumn(referencedColumnName="id", table="mailinglist")
> > > >     private MailingList mailingList;
> > > >
> > > > But openjpa w/ mysql does not generate a foreign key constraint:
> > > > mysql> show create table message;
> > > > | message | CREATE TABLE `message` (
> > > >   `id` bigint(20) NOT NULL,
> > > >   `state` smallint(6) default NULL,
> > > >   `version` int(11) default NULL,
> > > >   `mailingList_id` bigint(20) default NULL,
> > > >   PRIMARY KEY  (`id`),
> > > >   KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
> > > > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
> > > >
> > > > Am I missing something? (besides the constraint ;)
> > > >
> > > > Regards,
> > > >
> > > > KlaasJan
> > > >
> > > > Confidentiality Statement:
> > > >
> > > > This message is intended only for the individual or entity to
> which it
> > > is addressed. It may contain privileged, confidential information
> which is
> > > exempt from disclosure under applicable laws. If you are not the
> intended
> > > recipient, please note that you are strictly prohibited from
> disseminating
> > > or distributing this information (other than to the intended
> recipient) or
> > > copying this information. If you have received this communication in
> error,
> > > please notify us immediately by return email.
> > > > -----------------------------
> > > >
> > >
> >
>
> Confidentiality Statement:
>
> This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by return email.
> -----------------------------
>

RE: DDL for foreign key constraints.

Posted by CASERO Jaime <JC...@covansys.com>.
The parameter you are setting is the default for all foreign keys. This
means that "restrict" will be the action for all you foreign keys. In my
case, at least, this is not the desired behaviour. Sometimes I need
"restrict", and sometimes "cacacade" or "set null". Patrick, was the one
suggesting me "ForeignKey" annotation. Of course, as you said, this
locks you to OpenJPA.

If all you FK have the same action, then this property should do the
job.

Look at the mail I sent
http://mail-archives.apache.org/mod_mbox/openjpa-users/200708.mbox/%3cF2
E9537648B0EB428674C3B7B6D8834D05C0E906@fth-ex04.CVNS.corp.covansys.com%3
e

Try to override the action of your FK, using "OneToMany" or "ManyToOne"
annotations, with a different one than the "ForeignKeyDeleteAction"
property. I got always the same DDL for FKs -> setting actions to the
one set for the property.


-----Original Message-----
From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com] 
Sent: Friday, September 07, 2007 11:27 PM
To: users@openjpa.apache.org
Subject: Re: DDL for foreign key constraints.

That works, I didnt even have to use the @ForeignKey annotation as
suggested earlier.

KJ

On 9/7/07, Prashant Bhat <pr...@gmail.com> wrote:
> Not sure about your exact use case, but the following configuration
works
> for me (both with MappingTool to generate schema and at runtime with
the
> default operationOrderUpdateManager)
>
> -MappingDefaults
>
jpa(ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict)
>
> Hope this helps,
> Prashant
>
> On 9/7/07, klaasjan elzinga <kl...@gmail.com> wrote:
> >
> > Doesnt that tie me to the openjpa implementation? Is there a reason
> > why a foreign key constraint should not be created if the database
> > makes it possible, ie mysql / oracle etc.? Or is it specified by the
> > jpa specs that no foreign keys are to be made?
> >
> > Regards,
> >
> > KlaasJan
> >
> > On 9/7/07, CASERO Jaime < JCasero@covansys.com> wrote:
> > > I'm afraid you have to use "@ForeignKey" annotation to force JPA
to
> > > create the foreign key at DB level.
> > >
> > >
http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
> > > ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk
> > >
> > > Use this setting in your persistence.xml during development, to
set JPA
> > > to create the necessary Foreign Key, even if the schema was
already
> > > created.
> > >             <property name="openjpa.jdbc.SynchronizeMappings"
> > >                 value="buildSchema(ForeignKeys=true)"/>
> > >
> > > -----Original Message-----
> > > From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> > > Sent: Thursday, September 06, 2007 10:40 PM
> > > To: users@openjpa.apache.org
> > > Subject: DDL for foreign key constraints.
> > >
> > > Hi there,
> > >
> > > I am trying to get openjpa to generate foreign key constraints. I
have
> > > to following annotations:
> > >
> > > ... on the parent:
> > >
> > >     @OneToMany(targetEntity=Message.class,
cascade=CascadeType.ALL,
> > > mappedBy="mailingList")
> > >     private List<Message> messages = new ArrayList<Message>();
> > >
> > >
> > > ... on the child
> > >
> > >     @ManyToOne(targetEntity=MailingList.class, optional=false)
> > >     @JoinColumn(referencedColumnName="id", table="mailinglist")
> > >     private MailingList mailingList;
> > >
> > > But openjpa w/ mysql does not generate a foreign key constraint:
> > > mysql> show create table message;
> > > | message | CREATE TABLE `message` (
> > >   `id` bigint(20) NOT NULL,
> > >   `state` smallint(6) default NULL,
> > >   `version` int(11) default NULL,
> > >   `mailingList_id` bigint(20) default NULL,
> > >   PRIMARY KEY  (`id`),
> > >   KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
> > > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
> > >
> > > Am I missing something? (besides the constraint ;)
> > >
> > > Regards,
> > >
> > > KlaasJan
> > >
> > > Confidentiality Statement:
> > >
> > > This message is intended only for the individual or entity to
which it
> > is addressed. It may contain privileged, confidential information
which is
> > exempt from disclosure under applicable laws. If you are not the
intended
> > recipient, please note that you are strictly prohibited from
disseminating
> > or distributing this information (other than to the intended
recipient) or
> > copying this information. If you have received this communication in
error,
> > please notify us immediately by return email.
> > > -----------------------------
> > >
> >
>
 
Confidentiality Statement:
 
This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by return email.
-----------------------------

Re: DDL for foreign key constraints.

Posted by klaasjan elzinga <kl...@gmail.com>.
That works, I didnt even have to use the @ForeignKey annotation as
suggested earlier.

KJ

On 9/7/07, Prashant Bhat <pr...@gmail.com> wrote:
> Not sure about your exact use case, but the following configuration works
> for me (both with MappingTool to generate schema and at runtime with the
> default operationOrderUpdateManager)
>
> -MappingDefaults
> jpa(ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict)
>
> Hope this helps,
> Prashant
>
> On 9/7/07, klaasjan elzinga <kl...@gmail.com> wrote:
> >
> > Doesnt that tie me to the openjpa implementation? Is there a reason
> > why a foreign key constraint should not be created if the database
> > makes it possible, ie mysql / oracle etc.? Or is it specified by the
> > jpa specs that no foreign keys are to be made?
> >
> > Regards,
> >
> > KlaasJan
> >
> > On 9/7/07, CASERO Jaime < JCasero@covansys.com> wrote:
> > > I'm afraid you have to use "@ForeignKey" annotation to force JPA to
> > > create the foreign key at DB level.
> > >
> > > http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
> > > ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk
> > >
> > > Use this setting in your persistence.xml during development, to set JPA
> > > to create the necessary Foreign Key, even if the schema was already
> > > created.
> > >             <property name="openjpa.jdbc.SynchronizeMappings"
> > >                 value="buildSchema(ForeignKeys=true)"/>
> > >
> > > -----Original Message-----
> > > From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> > > Sent: Thursday, September 06, 2007 10:40 PM
> > > To: users@openjpa.apache.org
> > > Subject: DDL for foreign key constraints.
> > >
> > > Hi there,
> > >
> > > I am trying to get openjpa to generate foreign key constraints. I have
> > > to following annotations:
> > >
> > > ... on the parent:
> > >
> > >     @OneToMany(targetEntity=Message.class, cascade=CascadeType.ALL,
> > > mappedBy="mailingList")
> > >     private List<Message> messages = new ArrayList<Message>();
> > >
> > >
> > > ... on the child
> > >
> > >     @ManyToOne(targetEntity=MailingList.class, optional=false)
> > >     @JoinColumn(referencedColumnName="id", table="mailinglist")
> > >     private MailingList mailingList;
> > >
> > > But openjpa w/ mysql does not generate a foreign key constraint:
> > > mysql> show create table message;
> > > | message | CREATE TABLE `message` (
> > >   `id` bigint(20) NOT NULL,
> > >   `state` smallint(6) default NULL,
> > >   `version` int(11) default NULL,
> > >   `mailingList_id` bigint(20) default NULL,
> > >   PRIMARY KEY  (`id`),
> > >   KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
> > > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
> > >
> > > Am I missing something? (besides the constraint ;)
> > >
> > > Regards,
> > >
> > > KlaasJan
> > >
> > > Confidentiality Statement:
> > >
> > > This message is intended only for the individual or entity to which it
> > is addressed. It may contain privileged, confidential information which is
> > exempt from disclosure under applicable laws. If you are not the intended
> > recipient, please note that you are strictly prohibited from disseminating
> > or distributing this information (other than to the intended recipient) or
> > copying this information. If you have received this communication in error,
> > please notify us immediately by return email.
> > > -----------------------------
> > >
> >
>

Re: DDL for foreign key constraints.

Posted by Prashant Bhat <pr...@gmail.com>.
Not sure about your exact use case, but the following configuration works
for me (both with MappingTool to generate schema and at runtime with the
default operationOrderUpdateManager)

-MappingDefaults
jpa(ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict)

Hope this helps,
Prashant

On 9/7/07, klaasjan elzinga <kl...@gmail.com> wrote:
>
> Doesnt that tie me to the openjpa implementation? Is there a reason
> why a foreign key constraint should not be created if the database
> makes it possible, ie mysql / oracle etc.? Or is it specified by the
> jpa specs that no foreign keys are to be made?
>
> Regards,
>
> KlaasJan
>
> On 9/7/07, CASERO Jaime < JCasero@covansys.com> wrote:
> > I'm afraid you have to use "@ForeignKey" annotation to force JPA to
> > create the foreign key at DB level.
> >
> > http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
> > ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk
> >
> > Use this setting in your persistence.xml during development, to set JPA
> > to create the necessary Foreign Key, even if the schema was already
> > created.
> >             <property name="openjpa.jdbc.SynchronizeMappings"
> >                 value="buildSchema(ForeignKeys=true)"/>
> >
> > -----Original Message-----
> > From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> > Sent: Thursday, September 06, 2007 10:40 PM
> > To: users@openjpa.apache.org
> > Subject: DDL for foreign key constraints.
> >
> > Hi there,
> >
> > I am trying to get openjpa to generate foreign key constraints. I have
> > to following annotations:
> >
> > ... on the parent:
> >
> >     @OneToMany(targetEntity=Message.class, cascade=CascadeType.ALL,
> > mappedBy="mailingList")
> >     private List<Message> messages = new ArrayList<Message>();
> >
> >
> > ... on the child
> >
> >     @ManyToOne(targetEntity=MailingList.class, optional=false)
> >     @JoinColumn(referencedColumnName="id", table="mailinglist")
> >     private MailingList mailingList;
> >
> > But openjpa w/ mysql does not generate a foreign key constraint:
> > mysql> show create table message;
> > | message | CREATE TABLE `message` (
> >   `id` bigint(20) NOT NULL,
> >   `state` smallint(6) default NULL,
> >   `version` int(11) default NULL,
> >   `mailingList_id` bigint(20) default NULL,
> >   PRIMARY KEY  (`id`),
> >   KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
> > ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
> >
> > Am I missing something? (besides the constraint ;)
> >
> > Regards,
> >
> > KlaasJan
> >
> > Confidentiality Statement:
> >
> > This message is intended only for the individual or entity to which it
> is addressed. It may contain privileged, confidential information which is
> exempt from disclosure under applicable laws. If you are not the intended
> recipient, please note that you are strictly prohibited from disseminating
> or distributing this information (other than to the intended recipient) or
> copying this information. If you have received this communication in error,
> please notify us immediately by return email.
> > -----------------------------
> >
>

Re: DDL for foreign key constraints.

Posted by klaasjan elzinga <kl...@gmail.com>.
Doesnt that tie me to the openjpa implementation? Is there a reason
why a foreign key constraint should not be created if the database
makes it possible, ie mysql / oracle etc.? Or is it specified by the
jpa specs that no foreign keys are to be made?

Regards,

KlaasJan

On 9/7/07, CASERO Jaime <JC...@covansys.com> wrote:
> I'm afraid you have to use "@ForeignKey" annotation to force JPA to
> create the foreign key at DB level.
>
> http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
> ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk
>
> Use this setting in your persistence.xml during development, to set JPA
> to create the necessary Foreign Key, even if the schema was already
> created.
>             <property name="openjpa.jdbc.SynchronizeMappings"
>                 value="buildSchema(ForeignKeys=true)"/>
>
> -----Original Message-----
> From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com]
> Sent: Thursday, September 06, 2007 10:40 PM
> To: users@openjpa.apache.org
> Subject: DDL for foreign key constraints.
>
> Hi there,
>
> I am trying to get openjpa to generate foreign key constraints. I have
> to following annotations:
>
> ... on the parent:
>
>     @OneToMany(targetEntity=Message.class, cascade=CascadeType.ALL,
> mappedBy="mailingList")
>     private List<Message> messages = new ArrayList<Message>();
>
>
> ... on the child
>
>     @ManyToOne(targetEntity=MailingList.class, optional=false)
>     @JoinColumn(referencedColumnName="id", table="mailinglist")
>     private MailingList mailingList;
>
> But openjpa w/ mysql does not generate a foreign key constraint:
> mysql> show create table message;
> | message | CREATE TABLE `message` (
>   `id` bigint(20) NOT NULL,
>   `state` smallint(6) default NULL,
>   `version` int(11) default NULL,
>   `mailingList_id` bigint(20) default NULL,
>   PRIMARY KEY  (`id`),
>   KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
>
> Am I missing something? (besides the constraint ;)
>
> Regards,
>
> KlaasJan
>
> Confidentiality Statement:
>
> This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by return email.
> -----------------------------
>

RE: DDL for foreign key constraints.

Posted by CASERO Jaime <JC...@covansys.com>.
I'm afraid you have to use "@ForeignKey" annotation to force JPA to
create the foreign key at DB level.

http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/
ref_guide_mapping_jpa.html#ref_guide_mapping_jpa_fk

Use this setting in your persistence.xml during development, to set JPA
to create the necessary Foreign Key, even if the schema was already
created.
            <property name="openjpa.jdbc.SynchronizeMappings"
            	value="buildSchema(ForeignKeys=true)"/>  

-----Original Message-----
From: klaasjan elzinga [mailto:klaasjanelzinga@gmail.com] 
Sent: Thursday, September 06, 2007 10:40 PM
To: users@openjpa.apache.org
Subject: DDL for foreign key constraints.

Hi there,

I am trying to get openjpa to generate foreign key constraints. I have
to following annotations:

... on the parent:

    @OneToMany(targetEntity=Message.class, cascade=CascadeType.ALL,
mappedBy="mailingList")
    private List<Message> messages = new ArrayList<Message>();


... on the child

    @ManyToOne(targetEntity=MailingList.class, optional=false)
    @JoinColumn(referencedColumnName="id", table="mailinglist")
    private MailingList mailingList;

But openjpa w/ mysql does not generate a foreign key constraint:
mysql> show create table message;
| message | CREATE TABLE `message` (
  `id` bigint(20) NOT NULL,
  `state` smallint(6) default NULL,
  `version` int(11) default NULL,
  `mailingList_id` bigint(20) default NULL,
  PRIMARY KEY  (`id`),
  KEY `I_MESSAGE_MAILINGLIST` (`mailingList_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

Am I missing something? (besides the constraint ;)

Regards,

KlaasJan
 
Confidentiality Statement:
 
This message is intended only for the individual or entity to which it is addressed. It may contain privileged, confidential information which is exempt from disclosure under applicable laws. If you are not the intended recipient, please note that you are strictly prohibited from disseminating or distributing this information (other than to the intended recipient) or copying this information. If you have received this communication in error, please notify us immediately by return email.
-----------------------------