Hello guys,
I've a class UserGroup which contans a collection fo UserName instances. I would like to have these details persisted in single table as follows.
CREATE TABLE USER_GROUP
(
SYS_ID NUMBER NOT NULL,
GROUP_ID NUMBER NOT NULL,
GROUP_NAME VARCHAR2(30 BYTE),
FIRST_NAME VARCHAR2(30 BYTE),
LAST_NAME VARCHAR2(30 BYTE)
)
SYS_ID is auto-generated.
When I persist Usegroup class with a collection of 2 user name instances. Total 3 rows were created instead of 2 rows. First row is created with just GROUP_ID and GROUP_NAME and there after 2 rows with ID,GROUP_NAME,FIRST_NAME and LAST_NAME.
Can someone suggest a solution to get rid of empty row. Child table is highly discouraged as we have bunch of classes with this kind of functionality.
Thanks in advance.
Here are my classes.
import java.util.Collection;
/**
* @author Chandra
* $Log: UserGroup.java,v $
*/
public class UserGroup {
private Long id;
private String groupName;
//Collection of UserName objects;
private Collection userNames;
/**
* @return
*/
public String getGroupName() {
return groupName;
}
/**
* @return
*/
public Long getId() {
return id;
}
/**
* @return
*/
public Collection getUserNames() {
return userNames;
}
/**
* @param string
*/
public void setGroupName(String string) {
groupName = string;
}
/**
* @param long1
*/
public void setId(Long long1) {
id = long1;
}
/**
* @param collection
*/
public void setUserNames(Collection collection) {
userNames = collection;
}
/**
* @param o
* @return
*/
public boolean add(UserName o) {
return userNames.add(o);
}
/**
* @param o
* @return
*/
public boolean remove(UserName o) {
return userNames.remove(o);
}
public class UserName {
private String firstName;
private String lastName;
/**
* @return
*/
public String getFirstName() {
return firstName;
}
/**
* @return
*/
public String getLastName() {
return lastName;
}
/**
* @param string
*/
public void setFirstName(String string) {
firstName = string;
}
/**
* @param string
*/
public void setLastName(String string) {
lastName = string;
}
}
Here is mapping file.
<?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
package="com.test">
<class name="UserGroup" table="USER_GROUP">
<id name="id" column="GROUP_ID">
<generator class="sequence" >
<param name="sequence">GROUP_SEQ</param>
</generator>
</id>
<property name="groupName" column="GROUP_NAME"/>
<set name="userNames" cascade="all" lazy="false">
<key column="GROUP_ID"/>
<composite-element class="UserName">
<property name="firstName" column="FIRST_NAME" type="java.lang.String" not-null="true" />
<property name="lastName" column="LAST_NAME" type="java.lang.String" not-null="false" />
</composite-element>
</set>
</class>
</hibernate-mapping>
|