In my case I m using EntityManager, and I have to update collection type attribute in entities throuph the parent object:
<code>@MappedSuperClass
abstract class Entity{
private String uid;
@Transiant
private String typeCode = getClass().getSimpleName();
public void setAttribute(string attributeName, Object value){
//setting the attribute value wit reflection
}
public boolean isNew(){
// whether the Object is persisted or not yet
}
public <T> T get(String attributeName){
//generic getter for all attributes with reflection
}
}
class Order extends Entity{
//other attributes
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "order")
private List<OrderEntry> orderEntries = new ArrayList<>();
}
class OrderEntry extends Entity{
@ManyToOne(fetch = FetchType.EAGER)
private Order order;
}
</code>
<code>@MappedSuperClass
abstract class Entity{
private String uid;
@Transiant
private String typeCode = getClass().getSimpleName();
public void setAttribute(string attributeName, Object value){
//setting the attribute value wit reflection
}
public boolean isNew(){
// whether the Object is persisted or not yet
}
public <T> T get(String attributeName){
//generic getter for all attributes with reflection
}
}
class Order extends Entity{
//other attributes
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "order")
private List<OrderEntry> orderEntries = new ArrayList<>();
}
class OrderEntry extends Entity{
@ManyToOne(fetch = FetchType.EAGER)
private Order order;
}
</code>
@MappedSuperClass
abstract class Entity{
private String uid;
@Transiant
private String typeCode = getClass().getSimpleName();
public void setAttribute(string attributeName, Object value){
//setting the attribute value wit reflection
}
public boolean isNew(){
// whether the Object is persisted or not yet
}
public <T> T get(String attributeName){
//generic getter for all attributes with reflection
}
}
class Order extends Entity{
//other attributes
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "order")
private List<OrderEntry> orderEntries = new ArrayList<>();
}
class OrderEntry extends Entity{
@ManyToOne(fetch = FetchType.EAGER)
private Order order;
}
when I do something like :
<code>order = entityService.get("Order", uid);
newEntries = order.get("entries");
newEntries.removeAt(1);
order.setAttribute("entries", newEntries);
entityService.save(order);
</code>
<code>order = entityService.get("Order", uid);
newEntries = order.get("entries");
newEntries.removeAt(1);
order.setAttribute("entries", newEntries);
entityService.save(order);
</code>
order = entityService.get("Order", uid);
newEntries = order.get("entries");
newEntries.removeAt(1);
order.setAttribute("entries", newEntries);
entityService.save(order);
the collection in that object is not updated. but updating another attribute works fine.
the save method I need it that way so it has to work for all entities:
<code>@Transactional
public <T extends Entity> void save(T entity){
if(entity.isNew()){
entityManager.persist(entity)
}else{
entityManager.merge(entity)
}
}
</code>
<code>@Transactional
public <T extends Entity> void save(T entity){
if(entity.isNew()){
entityManager.persist(entity)
}else{
entityManager.merge(entity)
}
}
</code>
@Transactional
public <T extends Entity> void save(T entity){
if(entity.isNew()){
entityManager.persist(entity)
}else{
entityManager.merge(entity)
}
}