2010-07-09 72 views
1

我有使用JPA合併對象的問題。我的代碼是這樣的。jpa合併不起作用

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

我的實體是

@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; 

當我運行我的測試情況下,合併不更新我的FILTERNAME對象以及JPA不火在控制檯中的任何查詢。

任何人都可以請諮詢我做錯了什麼?

回答

2

你確定第一個查詢實際上是檢索記錄嗎?國際海事組織,這個查詢預計long作爲參數。

此外,由於你的實體不沾邊,我建議呼叫合併,讓JPA檢測和自動沖洗的變化:

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