-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Composite Foreign key, non primary composite unique keys ...
PostPosted: Thu Feb 14, 2008 11:52 pm 
Newbie

Joined: Thu Feb 14, 2008 10:43 pm
Posts: 8
I have some problems to implement relation OneToOne, ManyToOne, OneToMany in some situation.

Everything happens on a legacy database that I can not change (or can change only slightly)

First problem

Code:
TableA

Field1 PK
Field2 PK
Field3
Field4 UK
Field5 UK


Primary key is Field1+Field2 and it is implemented by EmbeddedId and a PK Class
Another unique key is Field4+Field5 and it is One Side for various Foreign key (OneToMany or even OneToOne).

I have added
Code:
@Table(
    name = "TableA",
    uniqueConstraints={
        @UniqueConstraint(
            columnNames={"Field4", "Field5"}
        )
    }
)

but then i did not understand how i should implement the key.

If i use (on TableB Entity)
Code:
    @JoinColumns({
        @JoinColumn(name = "Field1", referencedColumnName = "Field4", insertable = false, updatable = false),
        @JoinColumn(name = "Field2", referencedColumnName = "Field5", insertable = false, updatable = false)
    })
    @ManyToOne
    private TableA obj;

i obtain "not mapped to single property" error.

It is exactly the problem described on page 336 of the book.
But the book solution is with xml, instead i need solution with annotation (and possibly pure JPA annotation)

Second problem

Code:
TableA

Field1 PK, UK1, UK2, UK3
Field2 PK, UK1, UK2, UK3
Field3 PK
Field4 UK2
Field5 UK1, UK3
Field6 UK1
Field7 UK3



Primary Key is Field1+Field2+Field3
Alternative Unique Key1 is Field1+Field2 (non unique part of PK) + Field5+Field6
Alternative Unique Key2 is Field1+Field2 (non unique part of PK) + Field4
Alternative Unique Key3 is Field1+Field2 (non unique part of PK) + Field5+Field7

TableB FK references to TableA PK. Ordinary administration
TableC FK references to TableA UK1
TableD FK references to TableA UK2
TableE FK references to TableA UK3

What is the Entity model of TableA and its Keys ?

Third Problem

Code:
TableA

Field1 PK, UK1, UK2
Field2 PK, UK1, UK2
Field3 PK, UK1
Field4 PK, UK2



Primary Key is Field1+Field2+Field3+Field4 (but Field4 isn't necessary for the uniqueness)
Unique Key1 is Field1+Field2+Field3
Unique Key2 is Field1+Field2+Field4

Table A is a join table used to implement a ManyToMany relation for TableB with itself.
Field1+Field2+Field3 is TableB primary key and Field1+Field2+Field4 is yet table PK (but another record).

Field1+Field2+Field3+Field4 is Primary Key (on TableA) because TableC is on OneToOne relation with TableA using all four fields.

What is the Entity model design ?

Regards

Sorry for my not very good English


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.