Hibernate version:
2.1.4
Mapping documents:
<hibernate-mapping>
<class
name="com.ibm.cruser.model.User" table="users">
<id name="id" type="java.lang.Integer" column="id" unsaved-value="0" >
<generator class="increment" />
</id>
<property name="username" type="java.lang.String" column="username" not-null="true" length="30" >
</property>
<property name="password" type="java.lang.String" column="password" not-null="true" length="30">
</property>
other properties....
<!-- Associations -->
<!-- bi-directional one-to-many association to Member -->
<set
name="members" lazy="true" inverse="true" cascade="save-update">
<key>
<column name="userid" />
</key>
<one-to-many
class="com.ibm.cruser.model.Member" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class
name="com.ibm.cruser.model.Group"
table="groups"
>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="assigned" />
</id>
<property
name="groupname"
type="java.lang.String"
column="groupname"
not-null="true"
length="30"
>
</property>
<!-- Associations -->
<!-- bi-directional one-to-many association to Member -->
<set
name="members"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="groupid" />
</key>
<one-to-many
class="com.ibm.cruser.model.Member"
/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class
name="com.ibm.cruser.model.Member"
table="member"
>
<composite-id name="comp_id" class="com.ibm.cruser.model.MemberPK">
<meta attribute="field-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="userid"
column="userid"
type="java.lang.Integer"
length="4"
>
<meta attribute="field-description">
@hibernate.property
column="userid"
length="4"
</meta>
</key-property>
<key-property
name="groupid"
column="groupid"
type="java.lang.Integer"
length="4"
>
<meta attribute="field-description">
@hibernate.property
column="groupid"
length="4"
</meta>
</key-property>
</composite-id>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to Group -->
<many-to-one
name="group"
class="com.ibm.cruser.model.Group"
update="false"
insert="false"
>
<meta attribute="field-description">
@hibernate.many-to-one
update="false"
insert="false"
</meta>
<meta attribute="field-description">
@hibernate.column
name="groupid"
</meta>
<column name="groupid" />
</many-to-one>
<!-- bi-directional many-to-one association to User -->
<many-to-one
name="user"
class="com.ibm.cruser.model.User"
update="false"
insert="false"
>
<meta attribute="field-description">
@hibernate.many-to-one
update="false"
insert="false"
</meta>
<meta attribute="field-description">
@hibernate.column
name="userid"
</meta>
<column name="userid" />
</many-to-one>
<!-- end of derived association(s) -->
</class>
</hibernate-mapping>
public class User implements Serializable {
/** identifier field */
private Integer id;
/** persistent field */
private String username;
/** persistent field */
private String password;
With other properties, getters and setters, constructors...
}
public class Group implements Serializable {
/** identifier field */
private Integer id;
/** persistent field */
private String groupname;
/** persistent field */
private Set members;
With getters, setters and constructors
}
public class Member implements Serializable {
/** identifier field */
private com.ibm.cruser.model.MemberPK comp_id;
/** nullable persistent field */
private com.ibm.cruser.model.Group group;
/** nullable persistent field */
private com.ibm.cruser.model.User user;
/** full constructor */
public Member(com.ibm.cruser.model.MemberPK comp_id, com.ibm.cruser.model.Group group, com.ibm.cruser.model.User user) {
this.comp_id = comp_id;
this.group = group;
this.user = user;
}
/** default constructor */
public Member() {
}
/** minimal constructor */
public Member(com.ibm.cruser.model.MemberPK comp_id) {
this.comp_id = comp_id;
}
/**
* @hibernate.id
* generator-class="assigned"
*
*/
public com.ibm.cruser.model.MemberPK getComp_id() {
return this.comp_id;
}
public void setComp_id(com.ibm.cruser.model.MemberPK comp_id) {
this.comp_id = comp_id;
}
/**
* @hibernate.many-to-one
* update="false"
* insert="false"
*
* @hibernate.column
* name="groupid"
*
*/
public com.ibm.cruser.model.Group getGroup() {
return this.group;
}
public void setGroup(com.ibm.cruser.model.Group group) {
this.group = group;
}
/**
* @hibernate.many-to-one
* update="false"
* insert="false"
*
* @hibernate.column
* name="userid"
*
*/
public com.ibm.cruser.model.User getUser() {
return this.user;
}
public void setUser(com.ibm.cruser.model.User user) {
this.user = user;
}
public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof Member) ) return false;
Member castOther = (Member) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}
}
public class MemberPK implements Serializable {
/** identifier field */
private Integer userid;
/** identifier field */
private Integer groupid;
/** full constructor */
public MemberPK(Integer userid, Integer groupid) {
this.userid = userid;
this.groupid = groupid;
}
/** default constructor */
public MemberPK() {
}
/**
* @hibernate.property
* column="userid"
* length="4"
*
*/
public Integer getUserid() {
return this.userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
/**
* @hibernate.property
* column="groupid"
* length="4"
*
*/
public Integer getGroupid() {
return this.groupid;
}
public void setGroupid(Integer groupid) {
this.groupid = groupid;
}
public String toString() {
return new ToStringBuilder(this)
.append("userid", getUserid())
.append("groupid", getGroupid())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof MemberPK) ) return false;
MemberPK castOther = (MemberPK) other;
return new EqualsBuilder()
.append(this.getUserid(), castOther.getUserid())
.append(this.getGroupid(), castOther.getGroupid())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getUserid())
.append(getGroupid())
.toHashCode();
}
}
Code between sessionFactory.openSession() and session.close():
I'm using Spring Framework 1.1, Hibernate 2.1.4 wrapped within org.springframework.orm.hibernate.HibernateTransactionManager
here is the application-context.xml
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>org.postgresql.Driver</value></property>
<property name="url"><value>jdbc:postgresql:test</value></property>
<property name="username"><value>postgresql</value></property>
<property name="password"><value>jklcom</value></property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>com/ibm/cruser/model/User.hbm.xml</value>
<value>com/ibm/cruser/model/Group.hbm.xml</value>
<value>com/ibm/cruser/model/Member.hbm.xml</value>
<value>com/ibm/cruser/model/Title.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.query.substitutions">true=1 false=0</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="txProxyTemplate" lazy-init="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userDAO" class="com.ibm.cruser.dao.hibernate.UserDAOHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="userManagerTarget" class="com.ibm.cruser.service.impl.UserManagerImpl">
<property name="userDAO"><ref local="userDAO"/></property>
</bean>
<bean id="userManager" parent="txProxyTemplate">
<property name="target"><ref local="userManagerTarget"/></property>
</bean>
Full stack trace of any exception that occurs:
No exception
Name and version of the database you are using:
postgresql 7.4
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
I'm not getting an error, everything is working except for the collections. Here is the log when updating or adding a user. when a user has no role to start with, as you can see in the log message "get members: 0" indicating on roles at all. If a user starts out with one role, the log message will show "get members: 1). However and I submit the changes, nothing goes into the database.
[9/14/04 13:07:18:559 GMT-05:00] 89e7f6d UserFormContr I com.ibm.cruser.web.UserFormController Saving : jklcom : jklcom
[9/14/04 13:07:18:559 GMT-05:00] 89e7f6d UserFormContr I com.ibm.cruser.web.UserFormController get members: 0
[9/14/04 13:07:18:649 GMT-05:00] 89e7f6d SystemOut O Hibernate: update users set username=?, password=?, firstname=?, lastname=?, email=?, status=? where id=?
[9/14/04 13:07:18:699 GMT-05:00] 89e7f6d UserFormContr I com.ibm.cruser.web.UserFormController get new members: 0
|