Reputation: 135
I am trying to call Entitymanager.merge()
with a custom object but I keep getting the following error:
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of my.package.model.offer.Cost.id
java.lang.IllegalArgumentException: Can not set int field my.package.model.offer.Cost.id to java.lang.String
It seems like the getter of Cost.id
can't be called and inexplicably the returned value is a String
, but should be a int
.
This is my model class Cost
:
@Entity
public class Cost implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Lob
@Column(length = 65535)
private String description;
public Cost(String description) {
this.description = description;
}
public Cost() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
This is my controller (ManagedBean), which tries to call EntityManager.persist()
:
@ManagedBean(name = "offerController")
@SessionScoped
public class OfferController {
@PersistenceContext
private EntityManager em;
private Offer offerCurrent = new Offer();
public String saveCosts() {
offerCurrent.setCosts(costsCurrent);
try {
utx.begin();
offerCurrent = em.merge(offerCurrent);
utx.commit();
} catch (NotSupportedException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RollbackException e) {
e.printStackTrace();
} catch (HeuristicMixedException e) {
e.printStackTrace();
} catch (HeuristicRollbackException e) {
e.printStackTrace();
}
return Links.OFFERS.getLink();
}
}
This is the button on my JSF page, which calls the method saveCosts()
:
<p:commandButton action="#{offerController.saveCosts()}" value="Save" />
What could cause this problem and is there a way to solve it?
Upvotes: 2
Views: 1383
Reputation: 3593
You try to merge a new object. EntityManager#merge is only allowed for existing objects (According to the manual: IllegalArgumentException - if instance is not an entity or is a removed entity). The insert has to be done using EntityManager#persist. I suspect, you changed the code. So I can not tell what the original problem was.
OfferController is SessionScoped, are you using http-sessions? The Offer must be inserted(using persist), when the session begins and must be merged with later uses.
Upvotes: 1
Reputation: 3416
Your code makes no sense.
Please read a basic JPA tutorial as now you are calling the merge
and the persist
operation as well.
Persist is used in case you want an unmanaged entity to be managed by Hibernate and the entity state will be New.
Merge is used in case you have a detached entity and you want to attach it to the current persistence context with change propagation.
But you don't want to use both of them at the same time.
Upvotes: 0