-->
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.  [ 13 posts ] 
Author Message
 Post subject: Help with annotations -- Map
PostPosted: Sun Mar 12, 2006 5:52 pm 
Newbie

Joined: Sat Jan 28, 2006 1:10 pm
Posts: 6
Hi All,
I have a User(Entity), Project(Entity) and Role (enum). A User can have multiple roles within a project.

Hence in my User.java
--------------------------------------------------------------------------------------
@Entity
@Table(name="app_user")
public class User extends BaseObject implements Serializable
{
private static final long serialVersionUID = 3832626162173359411L;

private Integer id;
protected String username;
protected String password;
.....
protected Date lastModifiedDate;
protected Map<Project, Set<Role>> projectRoles = new HashMap<Project,Set<Role>>();

.........................
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinTable(name="user_project_role", joinColumns = { @JoinColumn( name="user_id") })
public Map<Project,Set<Role>> getProjectRoles()
{
return projectRoles;
}
.....
......
-------------------------------------------------------------------------------------

I get the following exception

org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.tacoma.model.User.projectRoles
[junit] at org.hibernate.cfg.annotations.CollectionBinder.getCollectionType(CollectionBinder.java:360)
[junit] at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:308)
[junit] at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1296)
[junit] at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:629)
[junit] at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:276)
[junit] at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:210)
[junit] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1156)


I understand why there is an error but I am stumped as to how to define the annotation part.

Any ideas?

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 8:08 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Collection of collections are not supported by Hibernate

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Using Map with a three way association
PostPosted: Fri Oct 06, 2006 9:06 am 
Newbie

Joined: Fri Oct 06, 2006 7:52 am
Posts: 5
I have almost the same problem (but without the need of Collections of Collections), if we consider that a User can have only one Role per Project.

hence I have a class like this :

public class User {


protected Map<Project,Role> roles ;

}


And three tables, one for Roles, one for Project, and one for Users. (as well as 3 classes).

The association User-Role-Project is described in another table with three foreign keys (a 3 tables association).

How can I annotate my field "roles" ? Is that possible with the JPA ? And using Hibernate capabilities ?

Thanks a lot for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 5:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
check the latest version this is possible in HAN (a spec extension though

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Doc Map<Entity,Entity> annotation with an association
PostPosted: Wed Oct 11, 2006 9:03 am 
Newbie

Joined: Fri Oct 06, 2006 7:52 am
Posts: 5
emmanuel wrote:
check the latest version this is possible in HAN (a spec extension though


Emmanuel,

Could you just point out where shall I look for in the Documentation ?

I am guessing it should be related to Hibernate-Annotations Documentation Reference "§ 2.4.6.3 Bidirectional association with indexed collections"

Should I use @MapKeyManyToMany or @FilterJoinTable , newly added to Hibernate ? If so, how ?

Just for the record, I repeat my problem (simplified) :

4 tables :

User
---------
User_Id, PK
UserName


Role
-------
Role_Id, PK
RoleName


Project
--------
Project_Id, PK
ProjectName


UserRoleProject (ternary association)
------------------
User_Id FK
Role_Id FK
Project_Id FK
(with the combination of the three FK being a PK for UserRoleProject)


Since a User can have only one Role per Project, the User class looks like this

class User {

@Id
@GeneratedValue
private Int userId ;

protected Map<Project,Role> roles ;
(i.e. "for this Project, the user has this Role")

protected String userName ;
...
}

and Project / Roles are simple POJOs.

The problem is : how can I annotate "roles" in order to use the (ternary) association table ?

Do I really need to create a class for the Entity UserRoleProject with a composite Id (EmbeddedId) which, by the way, is getting messy with the equals/hashCode problem (the tuples in the association table cannot be differentiated unless the User is persisted, in order to get the UserId) ?


Extra question :

What if in this association table, I have an extra colum (say, "mail", standing for "this User has this Role for this Project ; and for this Role, he should be reached at this e-mail address") ? How does the annotation change ?

(I had a look at http://www.hibernate.org/118.html#A10 but I didn't get it working with annotations)


Any help is greatly appreciated, I would love to avoid creating some specific classes just for getting the persistence working.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 11:19 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes @MapKeyManyToMany
If you need an extra column, you must map an intermediate entity

_________________
Emmanuel


Top
 Profile  
 
 Post subject: It worked !!!
PostPosted: Wed Oct 11, 2006 12:27 pm 
Newbie

Joined: Fri Oct 06, 2006 7:52 am
Posts: 5
Thanks a lot Emmanuel, I got it right !

I used :

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="UserRoleProject",
joinColumns={@JoinColumn(name="User_Id")},
inverseJoinColumns={@JoinColumn(name="Role_Id")})
@MapKeyManyToMany(joinColumns = @JoinColumn(name="Project_Id",unique = false))
protected Map<Project,Role> roles ;


even the "persist()" worked, without the problems I had with EmbeddedId !!!!

Too bad it's not in the EJB 3.0 specs.....

I am puzzled a bit, however... how about n-associations, with n>4 ? (Map of Map are not supported, right ?) I guess this is getting tough...

Thanks again !!!


Top
 Profile  
 
 Post subject: Re: It worked !!!
PostPosted: Tue Oct 17, 2006 4:33 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
notepad wrote:
I am puzzled a bit, however... how about n-associations, with n>4 ? (Map of Map are not supported, right ?) I guess this is getting tough...

Thanks again !!!


Map an intermediate entity which contains n many-to-one, you'll have a n-ary association

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Map MapKey and MapKeyManyToMany
PostPosted: Tue Oct 31, 2006 7:22 am 
Newbie

Joined: Fri Oct 06, 2006 7:52 am
Posts: 5
After having discovered how to solve my problem with @MapKeyManyToMany, I found a new odd one :


since I'm using @MapKeyManyToMany (see example in the previous post), I can't use @MapKey (mutually exclusive).

This seem to work fine as long as I run the program within Eclipse. When I'm launching it outside Eclipse, I get the following error :

Exception in thread "main" java.lang.ExceptionInInitializerError
at myproject.main.Application.main(Application.java:270)
Caused by: javax.persistence.PersistenceException: org.hibernate.AnnotationException: A Map must declare a @MapKey element
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:188)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:110)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
at com.eurocopter.cepe.lot2.bdd.SQL.DAOFactoryBDD.<clinit>(DAOFactoryBDD.java:39)
... 1 more
Caused by: org.hibernate.AnnotationException: A Map must declare a @MapKey element
at org.hibernate.cfg.annotations.MapBinder.bindKeyFromAssociationTable(MapBinder.java:63)
at org.hibernate.cfg.annotations.MapBinder.access$000(MapBinder.java:32)
at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:55)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:35)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1016)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:244)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1001)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:840)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:161)
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:624)
at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:126)
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:180)
... 5 more

Is this a bug ? Why does Hibernate ask for a MapKey when a MapKeyManyToMany is defined ? Or is it the JPA spec that requires that ?

My annotation is like this :

Quote:
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "CouleurAdaptateur", joinColumns = { @JoinColumn(name = "Id_GestionConfigBanc") }, inverseJoinColumns = { @JoinColumn(name = "Id_Def_Adaptateur") })
@MapKeyManyToMany(joinColumns = @JoinColumn(name = "CodeCouleur", unique = false))
private Map<Couleur, TAdaptateur> listeAdaptateurs = null;


This is very strange that I get the error only outside Eclipse (running through a .bat file)

I am using
-Eclipse Version : 3.2.0 ID génération : M20060629-1905
-Hibernate Annotations Version: 3.2.0.CR3
-Hibernate Core version: 3.2.0.cr5
-Hibernate EntityManager Version: 3.2.0.CR3
and EJB3-persistence version :
Quote:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.)
Built-By: hibernate.org
Specification-Title: EJB 3.0
Specification-Version: public-draft
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: EJB
Implementation-Version: public-draft March 22 2006
Implementation-Vendor: hibernate.org


Thanks for any help !


Top
 Profile  
 
 Post subject: Ooops
PostPosted: Tue Oct 31, 2006 10:28 am 
Newbie

Joined: Fri Oct 06, 2006 7:52 am
Posts: 5
My apologies, the problem didn't have anything to do with Hibernate.

I was (of course !) using by mistake a different version of Hibernate within Eclipse and outside Eclipse.


Top
 Profile  
 
 Post subject: Re: Ooops
PostPosted: Fri May 18, 2007 8:33 am 
Newbie

Joined: Fri May 18, 2007 8:21 am
Posts: 1
notepad wrote:
My apologies, the problem didn't have anything to do with Hibernate.

I was (of course !) using by mistake a different version of Hibernate within Eclipse and outside Eclipse.

Hi,

i've got the same problem with ternary associations!
Could you show me what do you do in the other classes?

Here you show the annotation that you used in class "User"
Code:
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="UserRoleProject",
joinColumns={@JoinColumn(name="User_Id")},
inverseJoinColumns={@JoinColumn(name="Role_Id")})
@MapKeyManyToMany(joinColumns = @JoinColumn(name="Project_Id",unique = false))


Is necessary use another kind off annotations in the others classes ("Role" and "Project")?

Sorry about my English :P


Top
 Profile  
 
 Post subject: Re: Help with annotations -- Map
PostPosted: Thu Jan 07, 2010 1:55 am 
Newbie

Joined: Thu Jan 07, 2010 1:50 am
Posts: 1
I am having one issue with this approach. For me the data is not getting persisted in Project table. In all other tables data is going fine.

I am new to Hibernate.

Can anyone help me.


Top
 Profile  
 
 Post subject: Re: Help with annotations -- Map
PostPosted: Thu Jan 26, 2012 1:07 pm 
Newbie

Joined: Thu Jan 26, 2012 11:56 am
Posts: 9
I had a similar trace but the problem was a default generation By NetBeans

Set param= new HashSet();

and sould be...

Set<MyEntity> param= new HashSet<MyEntity>();

and u sould change it in the getters and setters definition too.

Greetings

http://javahelp.redsaltillo.net

_________________
-------------------------------------------------------------
http://javahelp.redsaltillo.net


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