Strangely, I wrote a very small test program to show the error, but it works!!
Code:
public class Role {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Code:
import java.util.Set;
public class User {
private Long id;
private String name;
private Set<Role> roles;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Role" table="ROLE">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User" table="USER">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
<set name="roles" table="USER_ROLES">
<key column="USER_ID"/>
<many-to-many class="Role" column="ROLE_ID"/>
</set>
</class>
</hibernate-mapping>
Code:
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
public class Test {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put(Environment.URL, "jdbc:hsqldb:mem:ManyToManyTest");
properties.put(Environment.DRIVER, "org.hsqldb.jdbcDriver");
properties.put(Environment.USER, "sa");
properties.put(Environment.PASS, "");
properties.put(Environment.POOL_SIZE, "1");
properties.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
properties.put(Environment.CACHE_PROVIDER, "org.hibernate.cache.NoCacheProvider");
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
properties.put(Environment.TRANSACTION_STRATEGY, "org.hibernate.transaction.JDBCTransactionFactory");
properties.put(Environment.HBM2DDL_AUTO, "create");
properties.put(Environment.SHOW_SQL, "true");
Configuration cfg = new Configuration();
cfg.setProperties(properties);
cfg.addClass(Role.class);
cfg.addClass(User.class);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Transaction tx = sessionFactory.getCurrentSession().beginTransaction();
// First, lets create a few standard roles:
int nRoles = 5;
Set<Role> roles = new HashSet<Role>();
for (int i = 0; i < nRoles; i++) {
Role role = new Role();
role.setName("Role" + i);
session.save(role);
roles.add(role);
}
// Lets create a user with these roles:
User user = new User();
user.setName("user");
user.setRoles(roles);
session.save(user);
tx.commit();
}
}
The output:
Quote:
Hibernate: insert into ROLE (name, ID) values (?, ?)
Hibernate: call identity()
Hibernate: insert into ROLE (name, ID) values (?, ?)
Hibernate: call identity()
Hibernate: insert into ROLE (name, ID) values (?, ?)
Hibernate: call identity()
Hibernate: insert into ROLE (name, ID) values (?, ?)
Hibernate: call identity()
Hibernate: insert into ROLE (name, ID) values (?, ?)
Hibernate: call identity()
Hibernate: insert into USER (name, ID) values (?, ?)
Hibernate: call identity()
Hibernate: insert into USER_ROLES (USER_ID, ROLE_ID) values (?, ?)
Hibernate: insert into USER_ROLES (USER_ID, ROLE_ID) values (?, ?)
Hibernate: insert into USER_ROLES (USER_ID, ROLE_ID) values (?, ?)
Hibernate: insert into USER_ROLES (USER_ID, ROLE_ID) values (?, ?)
Hibernate: insert into USER_ROLES (USER_ID, ROLE_ID) values (?, ?)