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!