persistence@glassfish.java.net

Re: foreign key reference with toplink

From: Tom Ware <tom.ware_at_oracle.com>
Date: Wed, 05 Apr 2006 10:59:58 -0400

Hi Benjamin,

  You have probably seen an email from Marina Vatkina saying the current
version of DDL generation does not support foreign key generation.

  Is there anything besides the missing database constraint getting in
your way? If not, you should be able to proceeed since the constraint
is just a way of enforcing the relationship and the relationship can
exist without it. If you want, you can manually create the foreign key
constraint.

-Tom

Benjamin Graf wrote:

>Hi Tom,
>
>yes BO_ID is created in the historylogentity table, but no foreign key
>reference in the database. So outside the application there is no logical
>link between those tables.
>
>Greets, Benjamin
>
>Tom Ware wrote:
>
>
>>Hi Benjamin,
>>
>> In your example:
>>
>> @ManyToOne(cascade=CascadeType.ALL)
>> private BoEntity bo;
>>
>>I would expect the foreign key to be defaulted to BO_ID.
>>
>>What result are you seeing?
>>
>>-Tom
>>
>>Benjamin Graf wrote:
>>
>>
>>
>>>Hi Tom,
>>>
>>>I think both are necessary. I'm trying to create a relationship with two
>>>entities and I thought that toplink will "translate" it to foreign key
>>>reference as mentioned in the spec.
>>>
>>>Code example:
>>>
>>>@Entity
>>>@NamedNativeQuery(name="activeBO", query="select * from boentity where
>>>status=0", resultClass=BoEntity.class)
>>>public class BoEntity implements java.io.Serializable {
>>> @Id
>>> @GeneratedValue(strategy = GenerationType.AUTO)
>>> private Long id;
>>> @OneToMany(cascade=CascadeType.ALL, mappedBy="bo")
>>> private Collection<HistoryLogEntity> history; ...
>>>}
>>>
>>>@Entity
>>>public class HistoryLogEntity implements java.io.Serializable {
>>> @Id
>>> @GeneratedValue(strategy = GenerationType.AUTO)
>>> @Column(name="HistoryLogID")
>>> private Long id;
>>> @ManyToOne(cascade=CascadeType.ALL) private BoEntity bo;
>>> ...
>>>}
>>>
>>>Tom Ware wrote:
>>>
>>>
>>>
>>>
>>>>Hi Benjamin,
>>>>
>>>>It is not clear to me which of the following you are attempting.
>>>>
>>>>1. To create a OneToMany (and its corresponding ManyToOne) relationship
>>>>
>>>>
>>>>
>>>between two entities.
>>>
>>>
>>>
>>>
>>>>2. To automatically create tables from entities and have the database
>>>>
>>>>
>>>>
>>>foreign key constraint automatically generated.
>>>
>>>
>>>
>>>
>>>>Please provide a little more detail about what you are trying to do and
>>>>
>>>>
>>>>
>>>the code that you are using to set up your ManyToOne.
>>>
>>>
>>>
>>>
>>>>-Tom
>>>>
>>>>Benjamin Graf wrote:
>>>>
>>>>
>>>>
>>>>
>>>>>Hi,
>>>>>
>>>>>does anybody know how to create a foreign key reference with toplink? I
>>>>>tried several times with @OneToMany(mappedBy...) @ManyToOne, but I
>>>>>
>>>>>
>didn't
>
>
>>>>>succeed in creating a reference in the database table. Is that a
>>>>>
>>>>>
>>>>>
>>>limitation
>>>
>>>
>>>
>>>
>>>>>of toplink because there are no further information in the
>>>>>ejb3.0-persisitence-spec pdf-file.
>>>>>
>>>>>Regards, Benjamin
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
>
>