-->
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.  [ 2 posts ] 
Author Message
 Post subject: an interesting Hibernate / JPA Composite Key Question
PostPosted: Fri Jul 25, 2008 12:18 am 
Newbie

Joined: Tue Feb 13, 2007 8:23 pm
Posts: 2
I was wondering if anybody had a good way to solve this... I have it semi-working, but the join columns are read only? is there anyway to get the join columns insertable/updateable?

Say I have 3 tables called TableA, TableB, TableC

Table A & Table B are basic 3 column composite primary key... Say id, globalId, and globalProfile.

table a(
id int not null,
global_id int not null,
global_profile varchar(50)
)

table b(
id int not null,
global_id int not null,
global_profile varchar(50)
)

TableC also has a 3 column composite primary key id, globalId, and globalProfile, however there's also foreign keys to table A & table B which reuse 2 of the 3 columns in the primary key for the foreign key to each table.

table c(
id int not null,
table_a_id int not null,
table_b_id int not null,
global_id int not null,
global_profile varchar(50)
)

so table c(table_a_id, global_id, global_profile) maps to table a(id, global_id, global_profile)

so table c(table_b_id, global_id, global_profile) maps to table b(id, global_id, global_profile)

I'm using....
@ManyToOne
@JoinColumns({
@JoinColumn(name="table_a_id", referencedColumnName="id",insertable=false, updatable=false),
@JoinColumn(name="global_id", referencedColumnName="global_id", insertable=false, updatable=false),
@JoinColumn(name="global_profile", referencedColumnName="global_profile", insertable=false, updatable=false)
})

@ManyToOne
@JoinColumns({
@JoinColumn(name="table_b_id", referencedColumnName="id", insertable=false, updatable=false),
@JoinColumn(name="global_id", referencedColumnName="global_id", insertable=false, updatable=false),
@JoinColumn(name="global_profile", referencedColumnName="global_profile", insertable=false, updatable=false)
})

So anybody have any good ways to create mutable data... if i remove the insertable/updatable i get an exception about mixing modes. if i remove it across all elements i get sorry can't repeat columns in definition or something...

I understand this is a highly abnormal configuration, but the application requirements require this type of configuration since it's highly distributed data, that's also synchronizable across to a central location, and it needs to be able to co-exist.

so anybody have any ideas on how best to make it have a mutable configuration settings? The other option is leave it read only .. . then anytime i need to mutate it just do that in straight jdbc??

Thanks for the ideas...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 10:06 am 
Newbie

Joined: Thu Feb 14, 2008 10:43 pm
Posts: 8
I have had a similar problem but I haven't found out a clean and official solution.

There is fortunately a dirty (for me) trick that works.

You can map a DB Column to more then one Entity property, so you can create, on Table C entity, 2 new properties like these:

Code:
    @Column(name = "table_a_id", nullable = false, insertable = true, updatable = true)
    private int idTableA_2; //Workaround
    @Column(name = "table_b_id", nullable = false, insertable = true, updatable = true)
    private int idTableB_2; //Workaround


When you modify these properties the modifications are propagated to DB.

Of course, in your code should be careful to keep the values synchronized between 2 properties which point to the same column on db.

I.e.
There will be a idTableA_1 (used as readonly property for join) and a idTableA_2.
When you alter idTableA_2 you must alter idTableA_1 with same value also


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.