-->
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.  [ 3 posts ] 
Author Message
 Post subject: Multiple Relationships between two classes
PostPosted: Sat Mar 14, 2009 11:05 am 
Newbie

Joined: Sat Mar 14, 2009 10:48 am
Posts: 7
Hi,

I'm new to Hibernate and I'm having trouble creating 2 relationships between the same two classes. I can get both relationships working correctly independently, but when I try to combine them I'm getting errors.

Lets just say my first class is Items (for sale on an auction website) and my second class is Users

The first relationship is a OneToMany, since an Item for sale can have only one owner but a user can own many iterms

On User.java:

@OneToMany(mappedBy = "user")
public Set<Item> getOwnedItems(){
return ownedItems;
}

public void setOwnedItems(Set<Item> ownedItems){
this.ownedItems = ownedItems;
}

and on Item.java:

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "user_fk")
public User getOwner() {
return owner;
}

public void setOwner(User owner) {
this.owner = owner;
}

The second relationship between Item and User is many to many as I would like other Users to be able to register interest in an item thats for sale.

I've used the following specification for User.java:

@ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "users", fetch = FetchType.EAGER)
public Set<Item> getItems() {
return items;
}

public void setItems(Set<Item> items) {
this.items = items; }

and then on Item.java:

@ManyToMany(
cascade={CascadeType.ALL}
)
@JoinTable( name="user_item", joinColumns={@JoinColumn(name="item_fk")},
inverseJoinColumns={@JoinColumn(name="user_fk")}
)
public Set<User> getUsers()
{return users;}

public void setUsers(Set<User> users)
{this.users = users;}

As it stands, hibernate isnt even creating my database. I'm getting the following error back from the server:

Error creating bean with name 'org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor': Cannot create inner bean '(inner bean)' of type [org.springframework.transaction.interceptor.TransactionInterceptor] while setting bean property 'transactionInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext-services.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext-services.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.bm.model.Item.user in com.bm.model.User.ownedItems

If anyone could help me out with this, I'd really appreciate it. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 14, 2009 12:21 pm 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
ger_eire wrote:
On User.java:

@OneToMany(mappedBy = "user")
public Set<Item> getOwnedItems(){
return ownedItems;
}

public void setOwnedItems(Set<Item> ownedItems){
this.ownedItems = ownedItems;
}

and on Item.java:

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "user_fk")
public User getOwner() {
return owner;
}

public void setOwner(User owner) {
this.owner = owner;
}

The mappedBy should be "owner" not "user".

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 15, 2009 4:36 pm 
Newbie

Joined: Sat Mar 14, 2009 10:48 am
Posts: 7
That worked, thanks!


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