-->
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.  [ 2 posts ] 
Author Message
 Post subject: Assigning an existing entry to a new one
PostPosted: Tue Jun 12, 2012 7:26 am 
Newbie

Joined: Tue Jun 12, 2012 6:36 am
Posts: 1
Hi,

I'm new to Hibernate and datanbases and I'm trying to do my first steps with it.
I went through several tutorials but I'm still experiencing some problems.

I have two entities called "Customer" and "Address". Both should be stored in a many-to-one association.
I tried to assign the same address to several customers, so that i have i.e. 20 customers with the same address.
This works fine as long as i store all customers in one transaction. Doing so i have 20 entries in my Customer table and one entry in my Address table.
All 20 customers refer to the same address_id.

But when i try to save a customer with the same address in a following transaction the address is stored again as a second entry in the Address table and the new customer refers to the address_id of this new entry. This would mean, that if i store 50 customers that are all having the same address in 50 different transactions i will have the same address stored 50 times in my Address table.

I tried to make the Address an unique key, but then i get a "duplicate entry error".

So is there a possibility to let Hibernate check if the address i want to save is already existing in the database, and, if 'yes', automatically assign the correct address id to the customer, instead of saving the same address as a new entry?

Customer Entity
Code:
@Entity
@SequenceGenerator(name="customer_id", initialValue=1, allocationSize=1)
@Table(name="test.customer")
public class Customer {

Long customerId;
String name;
String emailAddress;
String contactNo;
Address address;

//Getters   
   
@Id
@GeneratedValue (strategy=GenerationType.AUTO)
@Column(name="customer_id", nullable=false)
public Long getCustomerId() {
return customerId;
}
@Column(name="name", nullable=false)
public String getName() {
return name;
}
@Column(name="email_id", nullable=false)
public String getEmailAddress() {
return emailAddress;
}
@Column(name="contact_no", nullable=false)
public String getContactNo() {
return contactNo;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="address_id")
public Address getAddress() {
return address;
}
   
// Setters   
   
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public void setName(String name) {
this.name = name;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public void setAddress(Address address) {
this.address = address;
}
}


Address Entity
Code:
@Entity
@SequenceGenerator(name="address_id", initialValue=1, allocationSize=1)
@Table(name="test.address")
public class Address {
long addressId;
String addressLine1;
String addressLine2;
String city;
String state;
Integer pincode;

//Getters

@Id
@GeneratedValue (strategy=GenerationType.AUTO)
@Column(name="address_id", nullable=false)
public long getAddressId() {
return addressId;
}
@Column(name="address_line1", nullable=false)
public String getAddressLine1() {
return addressLine1;
}
@Column(name="address_line2", nullable=false)
public String getAddressLine2() {
return addressLine2;
}
@Column(name="city", nullable=false)
public String getCity() {
return city;
}
@Column(name="state", nullable=false)
public String getState() {
return state;
}
@Column(name="pincode", nullable=false)
public Integer getPincode() {
return pincode;
}   



//Setters
   
public void setAddressId(long addressId) {
this.addressId = addressId;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public void setCity(String city) {
this.city = city;
}
public void setState(String state) {
this.state = state;
}
public void setPincode(Integer pincode) {
this.pincode = pincode;
}
}



Code:
HibernateUtil hU = new HibernateUtil();
SessionFactory sf = hU.getSessionFactory();
Session session = null;
Transaction transaction = null;

try {
session = sf.getCurrentSession();
transaction = session.beginTransaction();
session.save(customer);
session.save(address);
transaction.commit();
}catch(Exception e) {}



I tried to describe everything as good as possible. Please let me know if any information is missing.
And please don't be too harsh about my grammar, because english isn't my native language ;)


Thanks!


Top
 Profile  
 
 Post subject: Re: Assigning an existing entry to a new one
PostPosted: Tue Jun 12, 2012 10:17 am 
Newbie

Joined: Tue Jun 01, 2010 4:18 am
Posts: 11
Socio wrote:
Code:
HibernateUtil hU = new HibernateUtil();
SessionFactory sf = hU.getSessionFactory();
Session session = null;
Transaction transaction = null;

try {
session = sf.getCurrentSession();
transaction = session.beginTransaction();
session.save(customer);
session.save(address);
transaction.commit();
}catch(Exception e) {}




Code above would make customer and address persisted to database, but customer would have NULL value for address_id because it doesn't have reference to address. To make Customer have reference to address, use

Code:
// .. previous code ..
Address address = new Address();
address.setXXX(); // Set every property you need.
Customer customer = new Customer();
customer.setAddress(address);
session.save(customer);
// ... other code (transaction, exception, etc)


Quote:
I'm new to Hibernate and datanbases and I'm trying to do my first steps with it.
I went through several tutorials but I'm still experiencing some problems.

Do you really new to database and RDBMS at all? If so, please learn it well. IMO learning Hibernate/ORM is useless until you understand database/RDBMS well. You don't need to be an expert. But you need to know all aspect and basic knowledge about RDBMS world before entering ORM and things.

Hope this help.

Thanks,
xsalefter


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