2017-02-25 1 views
0

DB에 새로 유지 된 객체의 (임베디드 가능) composite-id를 가져 오려고합니다. 최대 절전 모드 4.3.5에서 mysql (myisam)을 사용하고 있습니다.새로운 지속 된 엔티티의 복합 ID 객체를 얻는 방법은 무엇입니까?

삽입 작업 자체가 완료되었지만 개체를 ​​플러시하고 트랜잭션을 커밋 한 경우에도 생성 된 Java 개체의 category-id는 항상 null입니다. 그러나 이것은 composite-id 객체에서만 발생합니다. 단일 ID 개체에서 항상 새로운 ID를 반환합니다.

내가 뭔가를 잊었습니까? 어떤 도움을 주셔서 감사합니다.

@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()); 

} 

카테고리 :

@Entity 
@Table(name = "category") 
public class Category implements ICombinedIdEntity { 

    @EmbeddedId 
    private CategoryId categoryId; 

........ 

카테고리 ID

@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; 
    } 

사전

의 JUnit에 감사드립니다3210

는 짧은 대답이 지원되지 않는 것입니다 방법

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; 
    } 

답변

1

저장 AbstractDao.

이것은 @EmbeddedId 식별자가 할당되어야하기 때문에 주석이 달린 @GeneratedValue 필드가 무시되는 이유를 설명하기 때문입니다.

일부 JPA 콜백을 사용하여 삽입시 값을 설정할 수 있지만 Out-of-the-Box에서는 최대 절전 모드에서는 이러한 값을 설정하지 않습니다.

관련 문제