tags:

views:

25

answers:

1

I have a problem with merging of objects using JPA. My code goes like this.

EntityTransaction entr = em.getTransaction();

entr.begin();

Query q = em
  .createQuery("SELECT filterName FROM IbFilterName filterName where"
    + " filterName.filterId = 42352 ");

List<IbFilterName> filterNameList = q.getResultList();

for(IbFilterName filterName : filterNameList){
   String values = filterName.getFilterValues();
   filterName.setFilterValues(values+"Saranya");
   em.merge(filterName);
}

entr.commit();

And my entity is

 @Id
 @SequenceGenerator(name = "IB_FILTER_NAMES_FILTERID_GENERATOR", sequenceName = "IB_LOGIN_HISTORY_SEQ")
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "IB_FILTER_NAMES_FILTERID_GENERATOR")
 @Column(name = "FILTER_ID")
 protected long filterId;

 @Column(name = "EXTERNAL_USER")
 protected String externalUser;

 @Column(name = "FILTER_NAME")
 protected String filterName;

 @Column(name = "FILTER_VALUES")
 protected String filterValues;

 // bi-directional many-to-one association to IbPortfolioFilterMapping
 @OneToMany(mappedBy = "ibFilterName")
 private Set<IbPortfolioFilterMapping> ibPortfolioFilterMappings;

When I run my test case, merge doesn't update my filtername object as well JPA does not fire any query in the console.

Can any one please advice what am I doing wrong??

+1  A: 

Are you sure that the first query is actually retrieving records? IMO, this query is expecting a long as parameter.

Also, since your entities are not detached, I suggest to NOT call merge and let JPA detect and flush the changes automatically:

em.getTransaction().begin();

Query q = em
  .createQuery("SELECT filterName FROM IbFilterName filterName WHERE"
    + " filterName.filterId = :id ").setParameter("id", 42352L);

List<IbFilterName> filterNameList = q.getResultList();

for(IbFilterName filterName : filterNameList){
    String values = filterName.getFilterValues();
    filterName.setFilterValues(values + "Saranya");
    //em.merge(filterName); // DON'T
}

em.getTransaction().commit();
Pascal Thivent