wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes EJB 3.0 - Mapping relationship between entity beans. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EJB 3.0 - Mapping relationship between entity beans." Watch "EJB 3.0 - Mapping relationship between entity beans." New topic
Author

EJB 3.0 - Mapping relationship between entity beans.

tan dung dang
Greenhorn

Joined: Oct 09, 2008
Posts: 6
Hi all pro,

Could you see my attach image file to understand easier my database.

My problem is : the money entity has relationship n-1 to User entity and Category entity. And in money table, my primary key is a combination of username, categoryname, date.
So, how can I map money with User and Category.

This following is my code (I removed setter method to reduce verbose code):

+ User:


+ Category :


+ Money:


+ MoneyPK:


+ Inserting:


When I run the main method of Inserting class, I have error: Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Could not determine type for: scbcd.entity.Category, for columns: [org.hibernate.mapping.Column(category)]

Thank a lot


[Thumbnail for budgetmanagement.jpg]

Krum Bakalsky
Ranch Hand

Joined: Mar 14, 2010
Posts: 46
Hi,

Are you relying on specific native Hibernate features ?

If not, then you are using classical JPA stuff.
And, at the first glance, i see a couple of disturbing things about your code.
# @Id
# @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.REFRESH})
# @JoinColumn(name="categoryname")
# public Category getCategory() {
#
# return category;
# }


I don't believe that this is correct. Since you have a "toMany" relationship, then your instance variable in your entity, should be collection-valued.
This means that you should use Collection, Set, List or Map. Here, you are using a single-valued reference to a Category entity, and this is definitely not correct.
Within the ManyToMany relationship, you have not specified a @JoinTable annotation, but only a @JoinColumn. This is also not correct.

Maybe you meant a @ManyToOne annotation ?

As far as i remember from Mike Keith's JPA 1.0 book, providers of JPA 1.0 are not required to support composite primary keys, which include relationship attributes.
Have you checked how Hibernate deals with this ?

SCJP 6 (86% - the hard way), SCBCD 5 (81% - the hard way)
tan dung dang
Greenhorn

Joined: Oct 09, 2008
Posts: 6
@Krum Bakalsky: thank you, in fact I tried to use #ManyToOne but it still isn't ok.
Krum Bakalsky
Ranch Hand

Joined: Mar 14, 2010
Posts: 46
What's the problem this time ?

Is Hibernate supporting compound primary keys that include relationship attributes ?
Laura Barroso
Ranch Hand

Joined: Sep 21, 2009
Posts: 30
I'm not an expert but I think that you should look at Category class and Money class:
# @OneToMany(mappedBy="category", cascade={CascadeType.ALL})
# public List<Money> getMoneys() {
#
# return moneys;
# }
//////////////////////////////////////////////////////////////////////////////
@Id
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name="categoryname")
public Category getCategory() {

return category;
}
When you said that a collection is mappedBy "category" JPA try to find it in the other end of the relationship, and when it find it the type is not the same... you have a List<Money> mapped by a Category object...I think that's the problem...
tan dung dang
Greenhorn

Joined: Oct 09, 2008
Posts: 6
Thank all of you,
I resolved this problem like the following:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3.0 - Mapping relationship between entity beans.