You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by George Christman <gc...@cardaddy.com> on 2012/03/21 21:29:42 UTC

Tapestry Hibernate session reattach

Hello, I'm wondering if it's possible to save an object to the Hibernates
session without committing the object to the database like so, 

session.save(myObject); 


Then return the page and reattach the session object. Everything I've tried
results in a null session object on page return although the object pk
continues to increment. I'm not sure if the hibernate session is really lost
or if the database is incrementing the pk. 



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5584040.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Hibernate session reattach

Posted by Lenny Primak <lp...@hope.nyc.ny.us>.
Have you tried EntityManager.detatch(entity) ?
Not sure about Hibernate but it works in JPA.

On Mar 21, 2012, at 4:29 PM, George Christman wrote:

> Hello, I'm wondering if it's possible to save an object to the Hibernates
> session without committing the object to the database like so, 
> 
> session.save(myObject); 
> 
> 
> Then return the page and reattach the session object. Everything I've tried
> results in a null session object on page return although the object pk
> continues to increment. I'm not sure if the hibernate session is really lost
> or if the database is incrementing the pk. 
> 
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5584040.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Hibernate session reattach

Posted by Giulio Micali <gi...@gmail.com>.
if i guess correctly, you are trying to do a reverse onetomany.

After a lot of mistakes, i reached this solution for that:

@Entity
@Table (name="M_INVOICE")
public class Invoice {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ID_INVOICE", nullable=false)
    private Long invoiceID;

    @OneToMany(fetch=FetchType.LAZY,mappedBy="invoice",orphanRemoval=true,
cascade={CascadeType.ALL})
*    @Column(name="ID_INVOICE") // means: this is a "reverse=true"
OneToMany (same as you can declare in a Hibernate HBM file)
*    private List<InvoiceDetail> dettagli;
}

i think you miss the part in bold, which make the list really a REVERSE
oneToMany.
here the InvoiceDetail:

@Entity
@Table (name="M_INVOICE_DETAIL")
@IdClass(InvoiceDetailPK.class)
public class InvoiceDetail implements Serializable {


    @Id
    private Integer detailID;
    @Id
    private Invoice invoice;

    .....

}

and the PKClass:

public class InvoiceDetailPK implements Serializable {


    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ID_INVOICE")
    private Fattura invoice;

    @Column(name="ID_DETAIL", nullable=false,insertable= true,
updatable=true)
    private Integer detailID;

   .....

}

and remember,* EQUALS and HASHCODE only on PK fields*.

With this solution, you can save the entity and the list associated even if
they where not previously in session.

Finally, instead of *persist* i use *save* or *merge*. (I use spring
HibernateTemplate, never used Session directly)

Hope this can help,
Giulio




2012/3/22 George Christman <gc...@cardaddy.com>

> Hi Giulio,
>
> Yes, that is what I'm looking for. The only reason I can't commit it at
> this
> point is due to validation exceptions. My root entity does have many others
> related.
>
> here's a snippet of my root entity.
>
> @Entity
> public class PurchaseRequest {
>
>    @Id
>    @GeneratedValue
>    @NonVisual
>    private Long id;
>
>    @Transient
>    private long tempId = UUID.randomUUID().getLeastSignificantBits();
>
>    @OneToMany(mappedBy = "purchaseRequest", cascade=CascadeType.ALL,
> orphanRemoval=true)
>    private List<LineItem> lineItems;
>
> and some of my page code.
>
> private PurchaseRequest pr;
>
> @Inject //hibernate session
> private Session session;
>
> Class<?> onActivate(PurchaseRequest pr) {
>    this.pr = pr;
> }
>
> void onValidate() {
>        if (form.getHasErrors()) {
>            session.persist(pr);
>        }
> }
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5586326.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Tapestry Hibernate session reattach

Posted by George Christman <gc...@cardaddy.com>.
Hi Giulio, 

Yes, that is what I'm looking for. The only reason I can't commit it at this
point is due to validation exceptions. My root entity does have many others
related. 

here's a snippet of my root entity. 

@Entity
public class PurchaseRequest {

    @Id
    @GeneratedValue 
    @NonVisual      
    private Long id;
    
    @Transient
    private long tempId = UUID.randomUUID().getLeastSignificantBits();

    @OneToMany(mappedBy = "purchaseRequest", cascade=CascadeType.ALL,
orphanRemoval=true)
    private List<LineItem> lineItems;

and some of my page code. 

private PurchaseRequest pr;

@Inject //hibernate session
private Session session;

Class<?> onActivate(PurchaseRequest pr) {
    this.pr = pr;
}

void onValidate() {
        if (form.getHasErrors()) {
            session.persist(pr); 
        }
}

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5586326.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Hibernate session reattach

Posted by Giulio Micali <gi...@gmail.com>.
Hi George,

What follows depends on my guess that you use some form of declarative
transaction mechanism, like annotations on methods.

Generally your transaction(and your session) are closed at the end of the
root method(annotated) call or at the end the request, if you use the
spring OpenSessionInViewFilter: so you always have a commit(or rollback) at
most at the end of the request.
The only exception i saw is SpringWebFlow, in which you can declare a
transaction on the entire flow (i don't know how they do).

If I remember correctly there are a lot of posts similar to your question
in hibernate forums, that says: reattaching the session without committing
is impossible.

for the UUID, i think you are talking about a sequence ?

I read you have a "root" entity, so i guess there are some others related:
could you post your root entity ?

Cheers,
Giulio



2012/3/22 George Christman <gc...@cardaddy.com>

> Forgot to post this in previous post. I'm getting the following exception
> when the page reloads and try's to get object from hibernate session.
>
> entities.PurchaseRequest Unable to convert client value '85' into an entity
> instance.
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5585948.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Tapestry Hibernate session reattach

Posted by George Christman <gc...@cardaddy.com>.
Forgot to post this in previous post. I'm getting the following exception
when the page reloads and try's to get object from hibernate session. 

entities.PurchaseRequest Unable to convert client value '85' into an entity
instance.

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5585948.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Hibernate session reattach

Posted by George Christman <gc...@cardaddy.com>.
Hi everyone, sorry about my wording, was very tired and practically bug eye'd
by the time I wrote that yesterday. Anyhow, my real world issue is as
followed. 

When serverside validation occurs and the application returns the page, I'm
having an issue with some interface UUID's not being passed to some of the
interface elements properly. My proposed solution was to persist the root
object to hibernate which would give me real primary keys on page return to
work with instead of the originally generated UUID's. I was under the
impression that I could persist my object to hibernate only when the form
contains validation errors and when the page returns "reloads" I could 
reattach my form to the session persisted at validation. I was hoping this
session would contain all my form data and real id's previously persisted at
validation. I did take a look at session.lock, not entirely sure I
understand it yet. 

I hope this better helps to clarify my issue. 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5585820.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Tapestry Hibernate session reattach

Posted by Kalle Korhonen <ka...@gmail.com>.
Your wording is a bit peculiar, but are you asking how to reattach an
object to a Hibernate session? You cannot "save" an object to a
session. When you call session.save(), you are INSERTing a new record.
If you want to reattach an object, the only way to do that in
Hibernate is with session.lock(...). See StaleStateExceptions?
paragraph on http://tynamo.org/tapestry-conversations+guide for more
info.

Kalle


On Wed, Mar 21, 2012 at 1:29 PM, George Christman
<gc...@cardaddy.com> wrote:
> Hello, I'm wondering if it's possible to save an object to the Hibernates
> session without committing the object to the database like so,
>
> session.save(myObject);
>
>
> Then return the page and reattach the session object. Everything I've tried
> results in a null session object on page return although the object pk
> continues to increment. I'm not sure if the hibernate session is really lost
> or if the database is incrementing the pk.
>
>
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-Hibernate-session-reattach-tp5584040p5584040.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org