persistence@glassfish.java.net

Re: foreign key reference with toplink

From: Benjamin Graf <Benjamin.Graf_at_gmx.net>
Date: Wed, 5 Apr 2006 16:35:07 +0200 (MEST)

Hi Tom,

yes a column called BO_ID is created in the historylogentity table, but it
is not linked with a foreign key reference in the database as you can see in
the sql statement created by toplink:

CREATE TABLE HISTORYLOGENTITY (HistoryLogID BIGINT NOT NULL, HistoryLogFrom
VARCHAR(255), HistoryLogTo VARCHAR(255), HistoryLogTransitionEnabled
VARCHAR(255), HistoryLogTime DATETIME, HistoryLogTransitionFired
VARCHAR(255), BO_ID BIGINT, PRIMARY KEY (HistoryLogID))
CREATE TABLE BOENTITY (ID BIGINT NOT NULL, STATUS TINYINT(1) default 0,
PLACES VARCHAR(255), TIME DATETIME, PETRI VARCHAR(255), OWNER_BusUserID
BIGINT, PRIMARY KEY (ID))
CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(38),
PRIMARY KEY (SEQ_NAME))
INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0)

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
>>>>
>>>>
>>>>
>>
>>
>>
>
>


-- 
Analog-/ISDN-Nutzer sparen mit GMX SmartSurfer bis zu 70%!
Kostenlos downloaden: http://www.gmx.net/de/go/smartsurfer