0

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;
    }
bemar
  • 334
  • 5
  • 11

1 Answers1

1

The short answer is this isn't supported.

This is because @EmbeddedId identifiers are meant to be assigned and thus explains why your annotated @GeneratedValue field is ignored.

You might be able to use some JPA callback in order to set the value on-insert but out-of-the-box, Hibernate won't attempt to set those values.

Naros
  • 19,928
  • 3
  • 41
  • 71