jpa merge not working

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??

Answers


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

Need Your Help

String to Zipped Stream in php

php file stream zip ziparchive

I have a processing server with my database and a serving database to serve up files with a low bandwidth cost. On the processing server, php is not able to create files so everything must be done ...

How do I set the label for JTextField?

java swing jlabel layout-manager

How to fix the label for Gateway, which is the only one not showing. All the rest are showing.