-->
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.  [ 4 posts ] 
Author Message
 Post subject: newb - save object and its member object also
PostPosted: Thu Aug 14, 2008 4:00 am 
Newbie

Joined: Thu Aug 14, 2008 3:53 am
Posts: 4
hi

im new and learning hib3.

I have 2 classes User and ContactDetails that have a one-to-one association.

User has a ContactDetails member.

I'm trying to save User and ContactDetails to the DB but only User is saved.

how do i get it to save contact details as well ?

User u = new User();
u.setAge(99);
u.setFirstname("bill");
u.setLastname("henry");
Contact c = new Contact();
c.setEmail("bob@world");
u.setContact(c);

HibernateUtil.getSessionFactory().getCurrentSession().save(u);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 4:05 am 
Newbie

Joined: Fri Jul 28, 2006 8:10 am
Posts: 5
could you post the sourcecode of the two classes?

are you using JPA or xml mapping files?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 4:12 am 
Newbie

Joined: Thu Aug 14, 2008 3:53 am
Posts: 4
here is the source, im using xml mapping files. Im not using any JPA



package events;

public class Contact {

private Long id;
String email;
User user;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}



}


//////////////////////////////////////////////////////////////////

package events;

import java.util.*;

public class User {

private Long id;
private int age;
private String firstname;
private String lastname;

private Contact contact;

public User() {}


public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}


private Set groups = new HashSet();



public Set getGroups() {
return groups;
}


public void setGroups(Set groups) {
this.groups = groups;
}


public void addToGroup(Group group) {
this.getGroups().add(group);
group.getParticipants().add(this);
}

public void removeFromGroup(Group group) {
this.getGroups().remove(group);
group.getParticipants().remove(this);
}


public Contact getContact() {
return contact;
}


public void setContact(Contact contact) {
this.contact = contact;
}



}

///////////////////////////////////////////////////////////////////////////////

<?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="events.User" table="USER">
<id name="id" column="USER_ID">
<generator class="native"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>

<set name="groups" table="USER_GROUP">
<key column="USER_ID"/>
<many-to-many column="GROUP_ID" class="events.Group"/>
</set>

<one-to-one name="contact" class="events.Contact"></one-to-one>



</class>

</hibernate-mapping>

///////////////////////////////////////////////////////////////////////////////

<?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="events.Contact" table="CONTACT" >
<id name="id" column="CONTACT_ID">
<generator class="native"/>
</id>
<property name="email" />

<one-to-one name="user" class="events.User"></one-to-one>

</class>


</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 14, 2008 4:32 am 
Newbie

Joined: Thu Aug 14, 2008 3:53 am
Posts: 4
i found that one way to do it is change the line in the user xml file

to be like this:

<one-to-one name="contact" class="events.Contact" cascade="all"></one-to-one>

which will cascade all changes saves deletes etc


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

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.