-->
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.  [ 13 posts ] 
Author Message
 Post subject: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 7:06 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Going through the link http://vladmihalcea.com/hibernate-facts-the-importance-of-fetch-strategy/, I notice two differences in behavior using Hibernate session directly vs through JPA entity manger.

(1) If I use hibernate session with (5.0.4) directly instead of through an JPA entitymanager for the "Use case 3: Selecting a list of Products with an explicit join fetch plan", I still see an extra select getting fired to fetch the company entity that is mapped as EAGER. Is this the expected behavior? If so, this is pretty confusing for the user, since the behavior is not standard across JPA and Hibernate. Are there any plans to standardize this?

(2) Regarding the OneToOne mapping in Product class:

Code:
   @OneToOne(mappedBy="product", cascade=CascadeType.ALL, fetch=FetchType.LAZY, optional=false)
   private WarehouseProductInfo warehouseProductInfo;


Iam unable to persist the product with hibernate (5.0.4) session directly. It throws following constraint violation exception
Code:
ERROR 23503: INSERT on table 'WAREHOUSEPRODUCTINFO' caused a violation of foreign key constraint 'SQL160116151600291' for key (0).  The statement has been rolled back.


Instead of @PrimaryKeyJoinColumn, I even tried with @MapsId in the WarehouseProductInfo class, even that fails with following exception.

Code:
org.hibernate.id.IdentifierGenerationException: null id generated for:class com.madhu.hibernate.fetchStrategy.WarehouseProductInfo


So wanted to know whether optional=false is supported or not in hibernate while persisting or am I doing somethig wrong?


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 8:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
1) I think you should open an issue for that.

2) You need to post the examples here to go through them.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 8:48 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Please find below the minimal executable code that replicates issue (2).
Post class
Code:
@Entity
public class Post implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;
   @OneToOne(mappedBy="post", cascade = { CascadeType.ALL }, optional=false)
   private PostDetails details;
   public long getId() {
      return id;
   }
   public PostDetails getDetails() {
      return details;
   }
   public void addDetails(PostDetails details) {
      this.details = details;
      details.setPost(this);
   }

   public void deleteDetails() {
      if(details !=null) {
         details.setPost(null);
      }
      this.details = null;
   }
}

that has OnetoOne mapping with PostDetails class below.
Code:
@Entity
public class PostDetails implements Serializable  {
   @Id
   @Column
   private long id;
   @OneToOne
   @JoinColumn(name="id")
   @MapsId
   private Post post;
   
   public long getId() {
      return id;
   }
   public Post getPost() {
      return post;
   }
   public void setPost(Post post) {
      this.post = post;
   }
}


And following is the test case to persist the entities.
Code:
   @Test
   public void persistPost() {
      Session session = sf.getCurrentSession();
      session.getTransaction().begin();
      Post post = new Post();
      PostDetails details = new PostDetails();
      post.addDetails(details);
      session.persist(post);
      session.beginTransaction().commit();
   }


This fails with following exception.

Code:
org.hibernate.id.IdentifierGenerationException: null id generated for:class com.madhu.hibernate.cascadeType.oneToOne.bidirectional.PostDetails


And if I try with @PrimaryKeyJoinColumn instead of @MapsId it fails with following exception.

Code:
Caused by: ERROR 23505: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL160110192140240' defined on 'POSTDETAILS'.


The only scenario in which I was able to persist is by removing optional=false or setting optional=true on the OneToOne mapping.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 10:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The problem is that the id should be Long not long.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 2:01 pm 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Vlad,

I tried with Long as well but still it gave me the same exception.

But, when I have changed the ID generator from
Code:
@GeneratedValue(strategy = GenerationType.IDENTITY)
to
Code:
@GeneratedValue(strategy = GenerationType.AUTO)
it worked with both optional=false/true. With AUTO it is using HIBERNATE_SEQUENCE to generate id.

Could you please see if it is AUTO or IDENTITY in your case. I suspect it could be AUTO.

I am not able figure out why it is working with AUTO and not with IDENTITY. So is there any such limitation on the IDENTITY generator or any technical reason?


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 4:11 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Sounds like a bug. Please add an issue and give me the link.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 4:59 pm 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Thanks for you support Vlad.

I have filed this as a bug and please find it at the link HHH-1054


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sat Jan 16, 2016 5:07 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Thanks a lot. Can you provide a test case as well, it's really easy. Check out our latest JPA templates.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sun Jan 17, 2016 1:38 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
Added the test case zip file to the JIRA. Appreciate the effort for making it so easy.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sun Jan 17, 2016 4:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Excellent. I'm glad you found it easy to write the test cases.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sun Jan 17, 2016 4:14 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
In fact, the same can be replicated with AUTO as well provided we make ID column in PostDetails table as a foreign key to ID column of Post table.

The exception now changes to

Code:
Referential integrity constraint violation: "FK_CIFB6RN1BV8O9W11HSW7YCWHE: PUBLIC.""ORMUNITTESTCASE$POSTDETAILS"" FOREIGN KEY(ID) REFERENCES PUBLIC.""ORMUNITTESTCASE$POST""(ID) (1)"


I have updated the JIRA accordingly. Also tested with Hibernate 4.3.11 and able to reproduce the issue.


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sun Jan 17, 2016 4:18 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
So it replicates with 5.x too, right?


Top
 Profile  
 
 Post subject: Re: Session Vs EntityManager difference with explicit JOIN HQL
PostPosted: Sun Jan 17, 2016 5:13 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
That's right, Vlad.


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

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.