• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 - Mapping relationship between entity beans.

 
tan dung dang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
budgetmanagement.jpg
[Thumbnail for budgetmanagement.jpg]
 
Krum Bakalsky
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
tan dung dang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Krum Bakalsky: thank you, in fact I tried to use #ManyToOne but it still isn't ok.
 
Krum Bakalsky
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the problem this time ?

Is Hibernate supporting compound primary keys that include relationship attributes ?
 
Laura Barroso
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank all of you,
I resolved this problem like the following:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic