-->
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.  [ 3 posts ] 
Author Message
 Post subject: One-to-One association with shared primary key
PostPosted: Thu Aug 27, 2009 10:47 am 
Newbie

Joined: Thu Aug 27, 2009 10:27 am
Posts: 1
i'm doing OneToOne association with shared primary key

Employee<--->Address (bidirectional)

I'm using Ms SQLServer 2005

Employee : EMP_ID(pK)
Address : ADDRESS_ID(pFK references EMP_ID of Employee)

I'm using hibernate with annotations.

Employee class :

@Entity
@Table(name = "EMPLOYEE")
public class Employee
{
@GenericGenerator(name = "assigned",strategy = "assigned")
@Id @GeneratedValue(generator = "assigned")
@Column(name = "EMP_ID")
private Integer empiId;
@Column(name = "NAME")
private String name;
@Column(name = "SALARY")
private Double salary;
@OneToOne
@PrimaryKeyJoinColumn
private Address address;
//.....setters n getter
}

Address class :

@Entity
@Table(name = "ADDRESS")
public class Address
{
@Id @GeneratedValue(generator = "myForeignGenerator")
@GenericGenerator(
name = "myForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property",value = "employee")
)
@Column(name = "ADDRESS_ID")
private Integer addressId;
@Column(name = "STREET")
private String street;
@Column(name = "CITY")
private String city;
@Column(name = "ZIPCODE")
private String zipcode;
@OneToOne(mappedBy = "address") // Is this stmt necessary??? (in the book 'Java Persistence with Hibernate', this line is not specified, if i dont write this line, i get an err.. -> "Could not determine type for: Package1.Employee, at table: ADDRESS, for columns: [org.hibernate.mapping.Column(employee)]", this is not my prime doubt)
private Employee employee;
//.......setters n getters
}

Main :

public static void main(String args[])
{
Employee employee = new Employee();
employee.setEmpiId(44);
employee.setName("AAA");
employee.setSalary(23000.0);

Address address = new Address();
address.setStreet("XZXZ");
address.setCity("Pune");
address.setZipcode("41004");

employee.setAddress(address);
address.setEmployee(employee);

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();

session.save(employee);
//session.save(address);

tx.commit();
session.close();
HibernateUtil.ShutDown();
}


I'm saving only the employee object.
But the changes get saved in db only in employee table.
It should also save the address object in the databse, right?
Or do i need to save the address object explicitly using session.save(address) after session.save(employee)?

Please help me.

Thanx in advance.


Top
 Profile  
 
 Post subject: Re: One-to-One association with shared primary key
PostPosted: Fri Aug 28, 2009 9:22 am 
Newbie

Joined: Tue Sep 09, 2008 11:35 am
Posts: 10
You need to set the cascade parameter on the @OneToOne annotation.
Like:
Code:
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "address")
.

Yes, the statement is necessary.


Top
 Profile  
 
 Post subject: Re: One-to-One association with shared primary key
PostPosted: Wed Dec 09, 2009 6:50 am 
Newbie

Joined: Fri Dec 04, 2009 11:40 am
Posts: 1
I know this was a little while ago now, but I hope you figured out that you want to put the cascade=CascadeType.ALL on the @OneToOne in the Employee class, if that's the object you want to save (you don't need it in the address class).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.