1

Question

Is it possible to do an insertion in Hibernate when there is the same object reference twice in a list, I want to insert both of them and the ID has to be unique?

Background

I'm using random-beans to generate a random object Person which has a list of type House. The problem is that random-beans doesn't create a new house object every time, it sometimes also uses existing references. In this case the same house object reference can be twice in the person's list. But regardless of random-beans I would like to handle multiple references of new objects that have to be committed within the same transaction in Hibernate.

Probably this is not possible by the assignment of the ID at Session.save(Object obj), as there is still the same object referenced in the list. If there is no trivial solution to this, I would also be thankful for a way to just drop duplicated objects on save or commit. Note that changing the List to a Set doesn't solve the problem, as the same reference can be in different lists.

Example

The database is a MySQL database.

Main

Person steven = new Person();
House house1 = new House();
House house2 = new House();
steven.setHouses(new ArrayList<House>(Arrays.asList(house1, house2, house1));

Session session = getSessionFactory().openSession();
session.beginTransaction();
session.save(steven); // this is where I need to generate 3 different IDs
session.getTransaction().commit(); // this is where a duplicate entry exception is thrown
session.close();

Person

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_person")
private int id;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<House> houses;

House

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_house")
private int id;
Community
  • 1
  • 1
Marvin
  • 9,164
  • 3
  • 26
  • 44

2 Answers2

0

session.persist() will create a new id for every object that you pass in. Please let me know if you hit issues.

Zeus
  • 6,386
  • 6
  • 54
  • 89
  • It shows the same behviour as `session.save()`. I think Hibernate does the right thing here, because I try to insert the same object twice. But I need a smart mechanism that does something like a convertion from "house1, house2, house1" into "house1, house2, house3". – Marvin Aug 01 '16 at 05:56
0

it is not possible, since you have just two instances. You need to create a copy or clone of house1

do something like : house3 = new House(house1);

alex
  • 8,904
  • 6
  • 49
  • 75
  • Thanks for your input. Unfortunately the code for populating the objects is not in my hands, as I use random-beans for this. So I'm in the situation where I get the an object tree in which somewhere are multiple instances of the same object and now I have to persist it. – Marvin Aug 01 '16 at 06:50
  • @Marvin unfortunately you can't. This is how hibernate works. – alex Aug 01 '16 at 07:08
  • Okay, I already expected this ;) For those who are interested in the random-beans issue, [see my issue report](https://github.com/benas/random-beans/issues/182). – Marvin Aug 01 '16 at 11:32