I have a simple entity that uses a generated primary key. If I simply persist the entity
every thing works in terms of the database however the generated primary key value is not
set in the class instance I have persisted. If I persist and then merge the result the
generated value is then set in my class instance however there are two row created in the
database one for the first scenario and then one for the second scenario. This implies to
me that the persist operation is creating an internally cached version of the class which
is then merged/copied when I call the merge operation.
Is there an operation I need to call after persist, or an additional annotation, or what,
in order to have the generated value appear in the class instance without having to call
merge.
Is the behaviour of the persist/merge combination a bug in that it creates two rows in the
database?
Here is the code for my entity.
@Entity (name = "P002Ledger")
@Table (name = "P002_LEDGER")
public class Ledger
implements Serializable
{
static final
private long serialVersionUID = -147023002293214496L;
static final
private Reporter _reporter = Reporter.getReporter(Ledger.class);
private long m_code;
private String m_name;
public Ledger ()
{
}
public Ledger (long code)
{
m_code = code;
}
public Ledger (String name)
{
m_name = name;
}
@Id
@SequenceGenerator(name="p002_ledger_code_seq", allocationSize=25)
@GeneratedValue(strategy=GenerationType.SEQUENCE,
generator="p002_ledger_code_seq")
@Column (name="ledger_code",nullable=false)
public long getCode()
{
return m_code;
}
protected void setCode(long code)
{
m_code = code;
}
@PostPersist
protected void askCode()
{
_reporter.trace("code = "+getCode());
}
@Column (name="ledger_name",nullable=false)
public String getName()
{
return m_name;
}
public void setName(String name)
{
m_name = name;
}
}
This is the code I have used to generate the persist/merge scenario. The service
methodsare simple wrappers around em.persist and em.merge.
Ledger ledger = new Ledger("Gen Barry");
service.insertLedger(ledger);
assertTrue(ledger.getCode() == 0);
ledger = service.updateLedger(ledger);
assertTrue(ledger.getCode() != 0);
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3964116#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...