-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping a class with single id to table with composite key
PostPosted: Tue Mar 11, 2008 12:37 pm 
Newbie

Joined: Tue Mar 11, 2008 7:52 am
Posts: 5
Hibernate version: 3.6.0 GA

Mapping documents: 3.3.0 GA

Name and version of the database you are using: MS SQL Server 2005 Express.

Hi all...!

And now, for something completly differente, I'm trying to map a composite key in a very special situation.

I have this previous conditions:

- Legacy database, that is forbidden to change.
- Domain model with POJOs that also cannot be modified.

And this is the problem that drives me crazy... First I have this class:

@Entity
public ExampleClass {

@Id
private Long id;

@ManyToOne
private A a;

@ManyToOne
private B b;

// lots of funny fields
}

An this is the table:

EXAMPLE_CLASS
--------------------
a_id (PK)
b_id (PK)
... (those funny columns)

As you can see, the table hasn't any id column and instead has a composite key using a and b primary keys.

Yeah, I will I could add the id column, but since I cannot modify the database model neither the domain model, it remains impossible.

What I'm looking for is a way - maybe tricky a one or even a dirty, antipattern or foolish one - for mapping or building that relation between the class and the table.

What I need - desperately - is a way that works.

Well, I have insulted you enough with my poor English. Thank you all, answers will be regarded with beer, if you are near Madrid (Spain). If not, I will send you an electronic pint.

Bye!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 12:30 pm 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
No problemo senior...:)

I hope I can help. You could do it this way:

A.class:

@Column(length = 50)
@EmbeddedId
private B id;

B.class:

@Embeddable
public class B implements Serializable {
private static final long serialVersionUID = 740233588519758936L;

private String propA;
private String propB;
private Long propC;
private String propD;

etc...

Hope this helps... regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 12:35 pm 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
hmm I might take it back.. Because using embeddedid will probablly cause it to be constructed in the table, and not as a reference as you mention you wanted...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 6:58 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
You can convert between database id and object identity with a user type.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 11:46 am 
Newbie

Joined: Tue Mar 11, 2008 7:52 am
Posts: 5
Thank you all...! :-D

Maybe it's my lucky day: they will add a PK key on that tables so I could map the classes well.


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

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.