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.  [ 1 post ] 
Author Message
 Post subject: Multiple rows representing single object.
PostPosted: Fri Apr 29, 2005 3:54 pm 
Newbie

Joined: Fri Apr 29, 2005 3:42 pm
Posts: 1
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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.