You're not doing anything wrong, per se.
The exception is pretty clear: Only entities attached to a Hibernate
Session can be persisted.
If you go a step further, it turns out that only entities which have
been previously committed to the db (ie: have a valid db id) can be
persisted with @Persist("entity"). One possibility would be to file
an "improvement" jira, suggesting that @Persist("entity") falls back
to storing the entire object in the session if it is new (no dbid) or
transient (not connected to a hibernate session). Otherwise, go back
to using @Persist alone (which defaults to session), as it will work
perfectly well for transient as well as non-transient objects, at the
cost of increased memory use for the session.
Robert
On Aug 15, 2008, at 8/154:37 AM , buckofive wrote:
Hi all,
I am having some problems creating a simple crud with tapestry-
hibernate
5.0.14. I followed the documentation and I have it mostly
working. I can
edit and save an existing entity with the @Persist("entity")
annotation but
if I try to create a new entity by setting the value to a newly
instantiated
entity I get this error:
# org.apache.tapestry5.runtime.ComponentEventException
Error persisting field PersistEntity:button: Failed persisting an
entity in
the session. Only entities attached to a Hibernate Session can be
persisted.
entity: [EMAIL PROTECTED]
context
eventType
setToTransient
# org.hibernate.TransientObjectException
object references an unsaved transient instance - save the transient
instance before flushing: com.roialte.entities.Button
I figured I was doing something wrong so I copied the
PersistEntity.tml and
PersistEntity.java into my project from the tapestry-hibernate
project.
loaded the page and clicked the "set to transient" link and I got
the same
error.
I am at a loss here as i cannot figure out what I'm doing wrong.
How do u
create a new entity for use within an edit page when using the
@Persist('entity') annotation? I am using tapestry versions 5.0.14
and
mysql 5.0 for my db. Any help is greatly appreciated.
Thanks!
here is a quick peek at my converted version of code from the
tapesty-hibernate project:
java:
public class PersistEntity
{
@Persist("entity")
@Property
private Button button;
@Inject
private ButtonDAO ButtonDAO;
@Inject
private Session session;
@Inject
private HibernateSessionManager manager;
void onCreateEntity()
{
Button button = new Button();
button.setName("name");
ButtonDAO.save(button);
this.button = button;
}
void onChangeName()
{
button.setName("name2");
// No commit, so no real change.
}
void onSetToTransient()
{
button = new Button();
}
void onSetToNull()
{
button = null;
}
void onDelete()
{
List<Button> buttons = ButtonDAO.findAll();
ButtonDAO.delete(buttons.toArray(new Button[0]));
}
}
@Entity
@Table(name = "button", catalog = "test", uniqueConstraints = {})
public class Button {
private Integer id;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false, insertable =
true,
updatable = true)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", unique = false, nullable = false, insertable
= true,
updatable = true, length=128)
@Validate("required,maxlength=128")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
tml:
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<body>
<p>entity name: <t:if test="button">${button.name}</t:if></p>
<p><t:eventlink event="createEntity">create entity</t:eventlink></p>
<p><t:eventlink event="changeName">change the name</t:eventlink></p>
<p><t:eventlink event="setToNull">set to null</t:eventlink></p>
<p><t:eventlink event="delete">delete</t:eventlink></p>
<p><t:eventlink event="setToTransient">set to transient</
t:eventlink></p>
</body>
</html>
--
View this message in context:
http://www.nabble.com/T5%3A-tapestry-hibernate-problem-when-setting-%40Persist%28%22entity%22%29-to-a-transient-object-tp18996169p18996169.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]