Hello, i have the problem in mapping tables using annotation @ManyToMany in a specific situation.
When you have a many-to-many relationship between entities and you use relation table containing only of foreign keys to avoid many-to-many relationship in your DB. The relation table has only 2 columns which are foreign keys from related tables, and it forms composite primary key.
For example:
USR
pk:id
FUNCT_PERMISSION
pk:id
GRANTED_PERMISSION
uid
pid
pk:(uid,pid)
I am able to do correct mapping with hbm.xml configuration, also i am able to work arround and do annotations mapping by adding surrogate primary key to GRANTED_PERMISSION table, then my annotation mapping uses only @ManyToOne annotations And it works, but whenever it is @ManyToMany it throws error:
Quote:
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: USR.Usr.functPermissions[USR.FunctPermission]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1068)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:600)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:541)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
Note i am using Hibernate 3.2.5I dont want to change DB cos in my DB i have 5 such situations and i dont want to use surrogate keys, and i dont want to use hbm.xml configurration i want to use annnotations.
My mappings are:
Usr.javaCode:
@ManyToMany(targetEntity=USR.FunctPermission.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinTable(name="GRANTED_PERMISSION", schema="dbo", catalog="USER_RIGHTS", joinColumns = {
@JoinColumn(name="USRID", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="FPERMID", nullable=false, updatable=false) })
public Collection<FunctPermission> getFunctPermissions() {
return this.functPermissions;
}
FunctPermission.javaCode:
@ManyToMany(mappedBy = "functPermissions", targetEntity= USR.Usr.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinTable(name="GRANTED_PERMISSION", schema="dbo", catalog="USER_RIGHTS", joinColumns = {
@JoinColumn(name="FPERMID", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="USRID", nullable=false, updatable=false) })
public Collection<Usr> getUsrs() {
return this.usrs;
}
hibernate.cfg.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=USER_RIGHTS</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">blahblah</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping class="USR.Helper"/>
<mapping class="USR.Usr"/>
<mapping class="USR.FunctPermission"/>
<mapping class="USR.HibernateUtil"/>
</session-factory>
</hibernate-configuration>
Is it Hibernate older version bug? Do i definetly need newer Hibernate version? How can i map correctly my classes with annotations, i am 3 days on it cannot find a reason. Help would be highly appreciated.