-->
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: Mapping m:n Senses? Insight Appreciated!
PostPosted: Mon Nov 03, 2008 3:30 am 
Newbie

Joined: Mon Oct 15, 2007 8:49 pm
Posts: 7
class Base
@Id
private Long oid;

class User extends Base
@OneToMany
private Set<UserRole> userRoles;

class UserRole extends Base
private int status;

@OneToOne
private User user;

@OneToOne
private Role role;


class Role extends Base
private int a;

Using hbm2ddl, I ended up with the following tables:
User
UserRole
User_UserRole
Role

I did not expect User_UserRole, what's that table for? From the DB perspective, I only need 3 tables, User, UserRole and Role.

Could someone shed some llights for me? My User insertion tests run fine and all tables are populated automatically by JPA/Hibernate, but why would there be a need for the User_UserRole table? How can I get rid of it?

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 1:10 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
I think whats happening here is Hibernate is expecting UserRole itself to act as an entity. Maybe entity isn't the right word, anyhow you don't have to create a class for a joining table, hibernate will handle that for you. If you do create a class for a joining table, hibernate thinks its a real class. By declaring a OneToMany relationship to it. Hibernate 'prefers' creating joining tables to any non-one-to-one relationships so if its generating your database it may be doing just that.

You should only have to create classes for User and Role, you will use UserRole in your mapping file but explicitly creating a class for it is probably making your tables generate in that strange way.

Cheers

Edit: I realized you're using annotations... I'll see if I can pull up an example for you.

Maybe this will help http://tadtech.blogspot.com/2007/09/hib ... -join.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 2:40 am 
Newbie

Joined: Mon Oct 15, 2007 8:49 pm
Posts: 7
I figured it out...

@Entity
class User

@Id
private Long oid;

@OneToMany(cascade={CascadeType.PERSIST}, mappedBy="user")
private Set<UserRole> userRoles

---

@Entity
class UserRole
@Id
private Long oid;

@ManyToOne
@JoinColumn(name="userOid", nullable=false)
private User user;

That's it...


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.