-->
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: [solved] @Id with @ManyToOne, and shared columns
PostPosted: Wed Jul 29, 2009 10:44 am 
Newbie

Joined: Wed Jul 29, 2009 9:55 am
Posts: 2
I'm trying currently to map the following schema (simplified to just the columns participating in the relations) to the following classes (also simplified), using annotations:

Code:
create table Domain (
id integer primary key
)

create table Category (
domain integer,
id integer,
primary key (domain, id),
foreign key (domain) references Domain (id)
)

create table Item (
domain integer,
id integer,
category integer,
primary key (domain, id),
foreign key (domain) references Domain (id),
foreign key (domain, category) references Category (domain, id)
)


Code:
class Domain {
Integer id;
}

class Category {
Domain domain;
Integer id;
}

class Item {
Domain domain;
Integer id;
Category category
}


So far my problems seem to be two:

One, I have a many-to-one relationship in my primary key. An hour's searching has told me that EJB2 didn't support @Id and @ManyToOne in its spec, hibernate might have, end EJB3 was supposed to. Also that by using update="false" insert="false" in some way I can get it to work, but I haven't actually found a description of the method. This isn't a serious problem in the end, however, since I'm willing to drop the Domain table and class and all the attributes they contain. As it stands, however, the domain column is being generated as a blob, which just isn't right.

Two, whether I drop Domain's attributes or not, there's still the sharing of the domain column between the foreign key and primary key. It seems like I should just be able to specify @JoinColumns, but I'm honestly not sure how Hibernate would handle it. And combining the solution to this problem to the previous is beyond my experience.

Thank you for your consideration, hopefully a solution will be found so I don't have to drop Hibernate.


Last edited by Gamen on Fri Jul 31, 2009 11:39 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: @Id with @ManyToOne, and sharing columns between objects
PostPosted: Fri Jul 31, 2009 11:32 am 
Newbie

Joined: Wed Jul 29, 2009 9:55 am
Posts: 2
For future users, here is the solution.

One, @IdClass does not permit relations, just like the JPA spec says, so use @EmbeddedId

Two, use @JoinColumns on the non-id field to name the column that should be shared. However, you'll have to use insertable=false, updatable=false, so it's a read-only relation.

Three, create a new field that will be mapped with the same column name as the unshared column of the non-id object that's read-only. Now, you just have to update the new field when you update the read-only object.

Thus:
Code:
@Entity
class Domain {
@Id
Integer id;
}

@Entity
class Category {
@EmbeddedId
DomainId domain_id;
}

@Entity
class Item {
@EmbeddedId
DomainId domain_id;
@ManyToOne
@JoinColumns({
  @JoinColumn(name="domain_id", referencedColumnName="domain_id", insertable=false, updatable=false),
  @JoinColumn(name="category_id", referencedColumnName="id", insertable=false, updatable=false)}
)
Category category;
Integer category_id;
}

@Embeddable
class DomainId {
@ManyToOne
Domain domain;
Integer id;
}


...Or something like that


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.