I'm trying to get the (Embeddable) composite-id of an object newly persisted to the DB. I'm using mysql (myisam) with hibernate 4.3.5.
The insert operation itself succedes, but the category-id of the created java object is always null even I flushed the object and commited the transaction. But this only happens with composite-id objects. In single id objects I always get the new id back.
Did I forget something? Any help is appreciated.
Thank you in advance
Ben
JUnit:
@Test
public void _1testInsertCombinedEntity() {
CategoryDao catDao = new CategoryDao();
Category cat = new Category();
CategoryId catId = new CategoryId();
catId.setCategoryCustomerId(1l);
cat.setCategoryId(catId);
cat.setCategoryName(catName);
cat.setCategoryDescr("Hello World. This is a test");
cat.setCategoryPrintable(true);
cat.setCategoryBookable(true);
cat = catDao.save(cat);
Assert.assertNotNull(cat.getCategoryId().getCategoryId());
}
Category:
@Entity
@Table(name = "category")
public class Category implements ICombinedIdEntity {
@EmbeddedId
private CategoryId categoryId;
........
CategoryId
@Embeddable
public class CategoryId implements Serializable, ICombinedId<Long, Long> {
/**
*
*/
private static final long serialVersionUID = -7448052477830797280L;
@Column(name = "category_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long categoryId;
@Column(name = "category_customer_id", nullable = false)
private Long categoryCustomerId;
public Long getCategoryId() {
return this.categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public Long getCategoryCustomerId() {
return this.categoryCustomerId;
}
public void setCategoryCustomerId(Long categoryCustomerId) {
this.categoryCustomerId = categoryCustomerId;
}
AbstractDao save method
protected T saveEntity(T entity) {
startTransaction();
System.out.println("Trying to save " + entity);
this.persistenceEngine.getEntityManager().persist(entity);
this.persistenceEngine.getEntityManager().flush();
commitCurrentTransaction();
clear();
return entity;
}