Reputation: 1516
I am new in Hibernate and trying to do something simple. Basically I have Person and Adress classes. Person class has adress object and I have Adress and Person db tables.
My question is that do I need to execute select statement and check if same adress is on the table everytime ? Or is there a way that Hibernate checks and insert if record does not exist ? I just want to use same Adress record when I insert another person on the second run of my application.
My classes:
@Entity
public class Person {
public Person(){
}
public Person(String name, int age, Adress adress){
this.name = name;
this.age = age;
this.adress = adress;
}
@Id
@GeneratedValue
private int id;
@Column(name = "NAME")
private String name;
@Column(name = "AGE")
private int age;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="address_id")
private Adress adress;
@Override
public boolean equals(Object other){
Person otherPrs = (Person)other;
return name.equals(otherPrs.name) && age == otherPrs.age && adress.equals(otherPrs.adress);
}
@Override
public int hashCode(){
int result = 31 * name.hashCode();
result += 31* Integer.hashCode(age);
result+=31*adress.hashCode();
return result;
}
Another one is:
@Entity
public class Adress {
@GeneratedValue
@Id
private int id;
@Column(name = "STREET")
private String street;
@Column(name = "CITY")
private String city;
public Adress(){
}
public Adress(String street, String city){
this.street = street;
this.city = city;
}
@Override
public boolean equals(Object other)
{
Adress otherAddr = (Adress)other;
return street.equals(otherAddr.street) && city.equals(otherAddr.city);
}
@Override
public int hashCode(){
int result = 31 * street.hashCode();
result += 31*city.hashCode();
return result;
}
And persisting the entities as :
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
try {
Adress adress = new Adress("emby", "USA");
Adress adress2 = new Adress("emby", "USA");
Person person = new Person("Hohn ", 31, adress);
Person person2 = new Person("Keyc", 21, adress2);
addPerson(sessionFactory ,person);
addPerson(sessionFactory ,person2);
//getPerson(sessionFactory, 1);
} finally {
sessionFactory.close();
}
}
private static void addPerson(SessionFactory sessionFactory, Person person){
Transaction tx = null;
Session session = null;
try {
session= sessionFactory.openSession();
tx = session.beginTransaction();
session.saveOrUpdate(person);
tx.commit();
}
catch (Exception e){
tx.rollback();
System.out.println("Exeception occured");
}
finally {
session.close();
}
}
With this code, there is 2 person record in Person table and 2 same address record with different id in Address table. I want 2 person in Person table point to same address record in database. I know that hibernate caches, but for the second run of the program, it adds 2 more record to db with same addresses.
Upvotes: 0
Views: 142
Reputation: 76
Saving an entirely new address for the same street and city is valid because you have not ensured that the combination of the two must be unique. Perhaps if you made a composite key out of street and city then hibernate would see that the unique entry already exists and would not save a new one.
Secondly, based on what you have described ("I want 2 person in Person table point to same address record in database."), Person to Address is not @OneToOne
, but @ManyToOne
.
Upvotes: 1