-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Entity loaded twice when calling em.find(entityId) correct?
PostPosted: Thu Mar 19, 2009 4:39 am 
Beginner
Beginner

Joined: Sat Feb 07, 2009 4:08 pm
Posts: 22
Location: New York City
When I call a find() method like:

Code:
UserSession usersession = em.find(userSessionId);


Hibernate is apparently loading the object twice and I don't understand why.

Could this be normal behaviour because the UserSession entity has a bidirectional OneToOne relationship with the User entity?

Here is the logging that shows the double loading, and the code for the two entity classes:

Code:

Hibernate:
    /* load com.mg.domain.UserSession */ select
        usersessio0_.USERSESSION_ID as USERSESS1_4_7_,
        usersessio0_.CREATED as CREATED4_7_,
        usersessio0_.NEXUS_ID as NEXUS6_4_7_,
        usersessio0_.QUESTION_ID as QUESTION7_4_7_,
        usersessio0_.QUESTION_POSTED as QUESTION3_4_7_,
        usersessio0_.SERVER_ID as SERVER4_4_7_,
        usersessio0_.UPDATED as UPDATED4_7_,
        usersessio0_.USER_ID as USER8_4_7_,
        nexus1_.NEXUS_ID as NEXUS1_0_0_,
        nexus1_.ACTIVE as ACTIVE0_0_,
        nexus1_.CREATED as CREATED0_0_,
        nexus1_.USER_ID as USER6_0_0_,
        nexus1_.QUESTION_ID as QUESTION7_0_0_,
        nexus1_.SCORE as SCORE0_0_,
        nexus1_.UPDATED as UPDATED0_0_,
        user2_.USER_ID as USER1_6_1_,
        user2_.CREATED as CREATED6_1_,
        user2_.EMAIL as EMAIL6_1_,
        user2_.EMAIL_UPDATED as EMAIL4_6_1_,
        user2_.ENABLED as ENABLED6_1_,
        user2_.FIRSTNAME as FIRSTNAME6_1_,
        user2_.LASTNAME as LASTNAME6_1_,
        user2_.PASS_UPDATED as PASS8_6_1_,
        user2_.PASSWORD as PASSWORD6_1_,
        user2_.UPDATED as UPDATED6_1_,
        user2_.USERNAME as USERNAME6_1_,
        user2_.ANSWERED as ANSWERED6_1_,
        user2_.CREDITS as CREDITS6_1_,
        user2_.SCORE as SCORE6_1_,
        user2_.ZIPCODE as ZIPCODE6_1_,
        question3_.QUESTION_ID as QUESTION1_5_2_,
        question3_.CATEGORY_ID as CATEGORY6_5_2_,
        question3_.CREATED as CREATED5_2_,
        question3_.TEXT as TEXT5_2_,
        question3_.TITLE as TITLE5_2_,
        question3_.UPDATED as UPDATED5_2_,
        question3_.USER_ID as USER7_5_2_,
        question4_.QUESTION_ID as QUESTION1_5_3_,
        question4_.CATEGORY_ID as CATEGORY6_5_3_,
        question4_.CREATED as CREATED5_3_,
        question4_.TEXT as TEXT5_3_,
        question4_.TITLE as TITLE5_3_,
        question4_.UPDATED as UPDATED5_3_,
        question4_.USER_ID as USER7_5_3_,
        category5_.CATEGORY_ID as CATEGORY1_1_4_,
        category5_.NAME as NAME1_4_,
        user6_.USER_ID as USER1_6_5_,
        user6_.CREATED as CREATED6_5_,
        user6_.EMAIL as EMAIL6_5_,
        user6_.EMAIL_UPDATED as EMAIL4_6_5_,
        user6_.ENABLED as ENABLED6_5_,
        user6_.FIRSTNAME as FIRSTNAME6_5_,
        user6_.LASTNAME as LASTNAME6_5_,
        user6_.PASS_UPDATED as PASS8_6_5_,
        user6_.PASSWORD as PASSWORD6_5_,
        user6_.UPDATED as UPDATED6_5_,
        user6_.USERNAME as USERNAME6_5_,
        user6_.ANSWERED as ANSWERED6_5_,
        user6_.CREDITS as CREDITS6_5_,
        user6_.SCORE as SCORE6_5_,
        user6_.ZIPCODE as ZIPCODE6_5_,
        user7_.USER_ID as USER1_6_6_,
        user7_.CREATED as CREATED6_6_,
        user7_.EMAIL as EMAIL6_6_,
        user7_.EMAIL_UPDATED as EMAIL4_6_6_,
        user7_.ENABLED as ENABLED6_6_,
        user7_.FIRSTNAME as FIRSTNAME6_6_,
        user7_.LASTNAME as LASTNAME6_6_,
        user7_.PASS_UPDATED as PASS8_6_6_,
        user7_.PASSWORD as PASSWORD6_6_,
        user7_.UPDATED as UPDATED6_6_,
        user7_.USERNAME as USERNAME6_6_,
        user7_.ANSWERED as ANSWERED6_6_,
        user7_.CREDITS as CREDITS6_6_,
        user7_.SCORE as SCORE6_6_,
        user7_.ZIPCODE as ZIPCODE6_6_
    from
        USERSESSION usersessio0_
    left outer join
        NEXUS nexus1_
            on usersessio0_.NEXUS_ID=nexus1_.NEXUS_ID
    left outer join
        USER user2_
            on nexus1_.USER_ID=user2_.USER_ID
    left outer join
        QUESTION question3_
            on nexus1_.QUESTION_ID=question3_.QUESTION_ID
    left outer join
        QUESTION question4_
            on usersessio0_.QUESTION_ID=question4_.QUESTION_ID
    left outer join
        CATEGORY category5_
            on question4_.CATEGORY_ID=category5_.CATEGORY_ID
    left outer join
        USER user6_
            on question4_.USER_ID=user6_.USER_ID
    left outer join
        USER user7_
            on usersessio0_.USER_ID=user7_.USER_ID
    where
        usersessio0_.USERSESSION_ID=?
Hibernate:
    /* load com.mg.domain.UserSession */ select
        usersessio0_.USERSESSION_ID as USERSESS1_4_7_,
        usersessio0_.CREATED as CREATED4_7_,
        usersessio0_.NEXUS_ID as NEXUS6_4_7_,
        usersessio0_.QUESTION_ID as QUESTION7_4_7_,
        usersessio0_.QUESTION_POSTED as QUESTION3_4_7_,
        usersessio0_.SERVER_ID as SERVER4_4_7_,
        usersessio0_.UPDATED as UPDATED4_7_,
        usersessio0_.USER_ID as USER8_4_7_,
        nexus1_.NEXUS_ID as NEXUS1_0_0_,
        nexus1_.ACTIVE as ACTIVE0_0_,
        nexus1_.CREATED as CREATED0_0_,
        nexus1_.USER_ID as USER6_0_0_,
        nexus1_.QUESTION_ID as QUESTION7_0_0_,
        nexus1_.SCORE as SCORE0_0_,
        nexus1_.UPDATED as UPDATED0_0_,
        user2_.USER_ID as USER1_6_1_,
        user2_.CREATED as CREATED6_1_,
        user2_.EMAIL as EMAIL6_1_,
        user2_.EMAIL_UPDATED as EMAIL4_6_1_,
        user2_.ENABLED as ENABLED6_1_,
        user2_.FIRSTNAME as FIRSTNAME6_1_,
        user2_.LASTNAME as LASTNAME6_1_,
        user2_.PASS_UPDATED as PASS8_6_1_,
        user2_.PASSWORD as PASSWORD6_1_,
        user2_.UPDATED as UPDATED6_1_,
        user2_.USERNAME as USERNAME6_1_,
        user2_.ANSWERED as ANSWERED6_1_,
        user2_.CREDITS as CREDITS6_1_,
        user2_.SCORE as SCORE6_1_,
        user2_.ZIPCODE as ZIPCODE6_1_,
        question3_.QUESTION_ID as QUESTION1_5_2_,
        question3_.CATEGORY_ID as CATEGORY6_5_2_,
        question3_.CREATED as CREATED5_2_,
        question3_.TEXT as TEXT5_2_,
        question3_.TITLE as TITLE5_2_,
        question3_.UPDATED as UPDATED5_2_,
        question3_.USER_ID as USER7_5_2_,
        question4_.QUESTION_ID as QUESTION1_5_3_,
        question4_.CATEGORY_ID as CATEGORY6_5_3_,
        question4_.CREATED as CREATED5_3_,
        question4_.TEXT as TEXT5_3_,
        question4_.TITLE as TITLE5_3_,
        question4_.UPDATED as UPDATED5_3_,
        question4_.USER_ID as USER7_5_3_,
        category5_.CATEGORY_ID as CATEGORY1_1_4_,
        category5_.NAME as NAME1_4_,
        user6_.USER_ID as USER1_6_5_,
        user6_.CREATED as CREATED6_5_,
        user6_.EMAIL as EMAIL6_5_,
        user6_.EMAIL_UPDATED as EMAIL4_6_5_,
        user6_.ENABLED as ENABLED6_5_,
        user6_.FIRSTNAME as FIRSTNAME6_5_,
        user6_.LASTNAME as LASTNAME6_5_,
        user6_.PASS_UPDATED as PASS8_6_5_,
        user6_.PASSWORD as PASSWORD6_5_,
        user6_.UPDATED as UPDATED6_5_,
        user6_.USERNAME as USERNAME6_5_,
        user6_.ANSWERED as ANSWERED6_5_,
        user6_.CREDITS as CREDITS6_5_,
        user6_.SCORE as SCORE6_5_,
        user6_.ZIPCODE as ZIPCODE6_5_,
        user7_.USER_ID as USER1_6_6_,
        user7_.CREATED as CREATED6_6_,
        user7_.EMAIL as EMAIL6_6_,
        user7_.EMAIL_UPDATED as EMAIL4_6_6_,
        user7_.ENABLED as ENABLED6_6_,
        user7_.FIRSTNAME as FIRSTNAME6_6_,
        user7_.LASTNAME as LASTNAME6_6_,
        user7_.PASS_UPDATED as PASS8_6_6_,
        user7_.PASSWORD as PASSWORD6_6_,
        user7_.UPDATED as UPDATED6_6_,
        user7_.USERNAME as USERNAME6_6_,
        user7_.ANSWERED as ANSWERED6_6_,
        user7_.CREDITS as CREDITS6_6_,
        user7_.SCORE as SCORE6_6_,
        user7_.ZIPCODE as ZIPCODE6_6_
    from
        USERSESSION usersessio0_
    left outer join
        NEXUS nexus1_
            on usersessio0_.NEXUS_ID=nexus1_.NEXUS_ID
    left outer join
        USER user2_
            on nexus1_.USER_ID=user2_.USER_ID
    left outer join
        QUESTION question3_
            on nexus1_.QUESTION_ID=question3_.QUESTION_ID
    left outer join
        QUESTION question4_
            on usersessio0_.QUESTION_ID=question4_.QUESTION_ID
    left outer join
        CATEGORY category5_
            on question4_.CATEGORY_ID=category5_.CATEGORY_ID
    left outer join
        USER user6_
            on question4_.USER_ID=user6_.USER_ID
    left outer join
        USER user7_
            on usersessio0_.USER_ID=user7_.USER_ID
    where
        usersessio0_.USER_ID=?



     EXIT UserSession FIND ******






Code:


@Entity
@Table(name = "USERSESSION")
public class UserSession implements Serializable, Comparable   {
   
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "USERSESSION_ID")
   private Long id;   
   
      
   //@Column(name = "USERNAME", length = 20, nullable = false, unique = true)
   @OneToOne
   @JoinColumn(name = "USER_ID")   
   private User user;
   
   @OneToOne
   @JoinColumn(name = "QUESTION_ID")   
   private Question question;
   
   @Temporal(TemporalType.TIMESTAMP)
   @Column(name="QUESTION_POSTED")   
   private Date questionPostDate ;
   
   @OneToOne
   @JoinColumn(name = "NEXUS_ID")   
   private Nexus nexus;
   
   @Temporal(TemporalType.TIMESTAMP)
   @Column(name="CREATED", nullable = false, updatable = false)   
   private Date created = new Date();
    
   @Temporal(TemporalType.TIMESTAMP)
   @Column(name="UPDATED", nullable = false, updatable = true)   
   private Date updated = new Date();
   
   @Column(name = "SERVER_ID", length = 30, nullable = false)   
   private String serverid;
   
   @Transient
   private transient int activeQuestionRank = -1;
   
   etc....
            


   
}





Code:

@Entity
@Table(name = "USER")
//@org.hibernate.annotations.Entity(selectBeforeUpdate = true)
public class User implements Serializable, Comparable   {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "USER_ID")
   private Long id;   
   
      
   @Column(name = "USERNAME", length = 20, nullable = false, unique = true)   
   private String username;
   
   @Column(name = "PASSWORD", length = 50, nullable = false, updatable = false)
   private String password;
   
   @Column(name = "FIRSTNAME", length = 50)
   private String firstname;
   
   @Column(name = "LASTNAME", length = 50)
   private String lastname;
   
   @Column(name = "EMAIL", length = 50, nullable = false)
   private String email;
   
   @Column(name = "ZIPCODE", length = 11)
   private String zipcode;
      
    @Temporal(TemporalType.TIMESTAMP)
   @Column(name="CREATED", nullable = false, updatable = false)   
    private Date created = new Date();
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="UPDATED", nullable = false, updatable = true)   
     private Date updated = new Date();
    
     @Temporal(TemporalType.TIMESTAMP)
    @Column(name="PASS_UPDATED", nullable = false, updatable = true)   
     private Date passupdated = new Date();
    
     @Temporal(TemporalType.TIMESTAMP)
    @Column(name="EMAIL_UPDATED", nullable = false, updatable = true)   
     private Date emailupdated = new Date();
      
      
   //fetch = FetchType.EAGER,
   @ManyToMany(
      targetEntity=com.mg.domain.Authority.class,      
       cascade={CascadeType.PERSIST, CascadeType.MERGE}
      )
   @JoinTable(
       name="USER_AUTHORITY",
       joinColumns = @JoinColumn( name="USER_ID"),
       inverseJoinColumns = @JoinColumn( name="AUTHORITY_ID")
   )   
    private Set<Authority> authorities = new HashSet<Authority>();
   
   
    @OneToMany(mappedBy="user")
    private Set<Question> questions = new HashSet<Question>();

    @OneToMany(mappedBy="createdby")
    private Set<Nexus> nexuses = new HashSet<Nexus>();

   
    @OneToOne(mappedBy = "user")
    private UserSession usersession;
   
   
   @Column(name = "ENABLED",  nullable = false)
    private boolean enabled = true;   
   
   @Embedded
   private UserStats userstats = new UserStats();
   
       
   public User() {}


 
}



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.