I’m using Hibernate 4.1.0.Final and hibernate-jpa-2.0-api. I’m having a problem using an entity manager to update an entity with a OneToMany relationship. Here is the entity:
@GenericGenerator(name = "uuid-strategy", strategy = "uuid.hex")
@Entity
@Table(name = "cb_organization", uniqueConstraints = {@UniqueConstraint(columnNames={"organization_id"})})
public class Organization implements Serializable
{
…
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinTable(name = "cb_organization_address",
joinColumns = @JoinColumn(name = "organization_id"),
inverseJoinColumns = @JoinColumn(name = "address_id"))
private List<Address> addresses;
When I try and update the entity (using the entity manager’s merge method) with a new one-to-many list, the addresses aren’t getting saved. I create the list like so …
List<Address> addresses = new ArrayList<Address>();
addresses.add(address);
...
org.setAddresses(addresses);
m_orgDao.save(org);
and here’s the relevant DAO code …
@Autowired
private EntityManager entityManager;
@Override
public Organization save(Organization organization)
{
if(StringUtils.isEmpty(organization.getId()))
{
entityManager.persist(organization);
}
else
{
organization = entityManager.merge(organization);
}
return organization;
}
The returned organization has an address in which all of the fields are null. What can I do differently with my entity manager and entities to make sure the data is persisted properly?
instead of