Код: Выделить всё
@Override
public void createInitialObjects() {
System.out.println("Create initial objects initiated");
//check whether the repo is empty
List objects = this.findAll();
if(objects.size() > 0) return;
System.out.println("Configuring some initial objects");
for(int i = 0; i < 6; i++) {
System.out.println("Cabins are being created");
//create and add a new object semi-random sample rate
System.out.println("Try ID");
// crashes on the em.persist if we use id=0L here;
// I do not understand why hibernate cannot persist newly created cabins....
Object object = Object.createSampleObject(-1L);
System.out.println("ID success");
this.save(object);
System.out.println("save" + object);
}
}
@Override
public Object findById(Long id) {
return em.find(Object.class, id);
}
@Override
public Object save(Object object) {
if (object.getId() == 0L) {
System.out.println("logg save by save");
// appears not to work on newly created cabins...
em.persist(object);
} else {
// return the combined, merged object
cabin = em.merge(object);
}
return object;
}
Код: Выделить всё
Hibernate:
call next value for cabin_sequence
save
Object{id=-1, type=SMALLODGE, numAvailable=4, description='colourful SmallDayTime objects, model 3', location='New York', image='/beachgear4.jpg', pricePerWeek=12}
Hibernate:
insert
into
object
(description, image, location, num_available, price_per_week, type, id)
values
(?, ?, ?, ?, ?, ?, ?)
Подробнее здесь: https://stackoverflow.com/questions/745 ... ave-object