2015-04-27 4 views
1

Рассмотрим следующий пример:Hibernate Коллекция Null на обновление

@Entity 
public class MainEntity { 


    @OneToOne(orphanRemoval = true, cascade = CascadeType.ALL) 
    private ChildEntity childEntity; 
} 

@Entity 
public class ChildEntity { 


    @OneToMany(cascade = CascadeType.ALL) 
    @LazyCollection(FALSE) 
    private List<AnotherEntity> otherEntities; 
} 

Теперь, когда я первый звонок

final ChildEntity anewChild = new ChildEntity(); 
    anewChild.addOtherEntity(anotherEntity); //Several Entities can be added here 
    mainEntity.setChildEntity(anewChild); 
EntityManager.persist(mainEntity); 

Все работает отлично, а потом я сделать некоторые обновления, вскоре после завершения транзакции ,

final ChildEntity anotherNewChild = new ChildEntity(); 
anotherNewChild.addOtherEntity(anotherEntity); //Several Entities can be added here 
mainEntity.setChildEntity(anotherNewChild); 

//A log of LOG.info(mainEntity); shows all fields appropriately set 
//At some point during merge operation, the new ChildEntity will need to be persisted. 
//According to my logs, an invocation of EntityManager.persist(anotherNewChild) occurs, during as the merge is propagated to the new entity. 
//At this point is where the ChildEntity.otherEntities is detected to be null 
return EntityManager.merge(mainEntity); 

Проблема заключается в том, что, с упорствовать, то

List<AnotherEntity>

не равно нулю и не пусто, а на слиянии, то

List<AnotherEntity>

is null

Я делаю это над удаленным вызовом ejb.

Hibernate 4.3.6 wildfly 8.1.0 JPA 2.1

Есть ли что-то я здесь отсутствует?

Воспроизводится проблема со следующим кодом:

https://github.com/marembo2008/hibernate-jpa-bug

Открыт вопрос о спящем Issue Tracker.

https://hibernate.atlassian.net/browse/HHH-9751

ответ

0

Проблема заключается в том, что слияние возвращает новый объект, так что вы должны сделать что-то вроде этого:

mainEntity = EntityManager.merge(mainEntity); 
+0

я делаю именно это. Поскольку я делаю удаленный вызов ejb, я бы не возвращал ничего, кроме этого. См. Мое редактирование на вопрос – maress