Hibernate Fetch is not longer sub-type of Join










3















I'm using Hibernate 5.2.10 with dynamic criteria and find out that Fetch is not longer assignable to Join.



In the next example, I need to fetch group data in the same query, but I also need to use the group's field for sorting or restriction.



CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<GroupAssignment> query = cb.createQuery(GroupAssignment.class);

Root<GroupAssignment> root = query.from(GroupAssignment.class);
SingularAttributeJoin<GroupAssignment, Group> groupFetch = (SingularAttributeJoin<GroupAssignment, Group>) root.fetch(GroupAssignment_.group, JoinType.LEFT);
query.orderBy(cb.asc(groupFetch.get(Group_.title)));


I have manually cast Fetch to the SingularAttributeJoin and after that, I can use get method for the ordering purpose, but I'm looking for the right way how I can do that without casting manually.



GroupAssignment class:



@Entity()
@Table(name = "group_assignment")
public class GroupAssignment
@Id
@Column(name = "group_assignment_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "group_id", nullable = false)
private Group group;

//other fields, getters and setters



GroupAssignment_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(GroupAssignment.class)
public abstract class GroupAssignment_
public static volatile SingularAttribute<GroupAssignment, Integer> id;
public static volatile SingularAttribute<GroupAssignment, Group> group;



Group class:



@Entity()
@Table(name = "navigation_group")
public class Group
@Id
@Column(name = "group_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "title")
private String title;

//other fields, getters and setters



Group_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Group.class)
public abstract class Group_
public static volatile SingularAttribute<Group, Integer> id;
public static volatile SingularAttribute<Group, String> title;










share|improve this question
























  • Can you put the code where you try to cast?

    – michaeak
    Nov 13 '18 at 12:20











  • It is already present in the original description (casting to the SingularAttributeJoin)

    – Deplake
    Nov 13 '18 at 12:22












  • Is this CriteriaBuilder the Hibernate-own CriteriaBuilder? Because there is a javax.persistence.criteria.CriteriaBuilder which is quite well documented

    – michaeak
    Nov 13 '18 at 12:26











  • Yes, it is javax.persistence.criteria.CriteriaBuilder

    – Deplake
    Nov 13 '18 at 12:30











  • Ok, good. Is it possible to post the Group, GroupAssignment, Group_ and GroupAssignment_ classes?

    – michaeak
    Nov 13 '18 at 12:36















3















I'm using Hibernate 5.2.10 with dynamic criteria and find out that Fetch is not longer assignable to Join.



In the next example, I need to fetch group data in the same query, but I also need to use the group's field for sorting or restriction.



CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<GroupAssignment> query = cb.createQuery(GroupAssignment.class);

Root<GroupAssignment> root = query.from(GroupAssignment.class);
SingularAttributeJoin<GroupAssignment, Group> groupFetch = (SingularAttributeJoin<GroupAssignment, Group>) root.fetch(GroupAssignment_.group, JoinType.LEFT);
query.orderBy(cb.asc(groupFetch.get(Group_.title)));


I have manually cast Fetch to the SingularAttributeJoin and after that, I can use get method for the ordering purpose, but I'm looking for the right way how I can do that without casting manually.



GroupAssignment class:



@Entity()
@Table(name = "group_assignment")
public class GroupAssignment
@Id
@Column(name = "group_assignment_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "group_id", nullable = false)
private Group group;

//other fields, getters and setters



GroupAssignment_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(GroupAssignment.class)
public abstract class GroupAssignment_
public static volatile SingularAttribute<GroupAssignment, Integer> id;
public static volatile SingularAttribute<GroupAssignment, Group> group;



Group class:



@Entity()
@Table(name = "navigation_group")
public class Group
@Id
@Column(name = "group_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "title")
private String title;

//other fields, getters and setters



Group_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Group.class)
public abstract class Group_
public static volatile SingularAttribute<Group, Integer> id;
public static volatile SingularAttribute<Group, String> title;










share|improve this question
























  • Can you put the code where you try to cast?

    – michaeak
    Nov 13 '18 at 12:20











  • It is already present in the original description (casting to the SingularAttributeJoin)

    – Deplake
    Nov 13 '18 at 12:22












  • Is this CriteriaBuilder the Hibernate-own CriteriaBuilder? Because there is a javax.persistence.criteria.CriteriaBuilder which is quite well documented

    – michaeak
    Nov 13 '18 at 12:26











  • Yes, it is javax.persistence.criteria.CriteriaBuilder

    – Deplake
    Nov 13 '18 at 12:30











  • Ok, good. Is it possible to post the Group, GroupAssignment, Group_ and GroupAssignment_ classes?

    – michaeak
    Nov 13 '18 at 12:36













3












3








3








I'm using Hibernate 5.2.10 with dynamic criteria and find out that Fetch is not longer assignable to Join.



In the next example, I need to fetch group data in the same query, but I also need to use the group's field for sorting or restriction.



CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<GroupAssignment> query = cb.createQuery(GroupAssignment.class);

Root<GroupAssignment> root = query.from(GroupAssignment.class);
SingularAttributeJoin<GroupAssignment, Group> groupFetch = (SingularAttributeJoin<GroupAssignment, Group>) root.fetch(GroupAssignment_.group, JoinType.LEFT);
query.orderBy(cb.asc(groupFetch.get(Group_.title)));


I have manually cast Fetch to the SingularAttributeJoin and after that, I can use get method for the ordering purpose, but I'm looking for the right way how I can do that without casting manually.



GroupAssignment class:



@Entity()
@Table(name = "group_assignment")
public class GroupAssignment
@Id
@Column(name = "group_assignment_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "group_id", nullable = false)
private Group group;

//other fields, getters and setters



GroupAssignment_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(GroupAssignment.class)
public abstract class GroupAssignment_
public static volatile SingularAttribute<GroupAssignment, Integer> id;
public static volatile SingularAttribute<GroupAssignment, Group> group;



Group class:



@Entity()
@Table(name = "navigation_group")
public class Group
@Id
@Column(name = "group_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "title")
private String title;

//other fields, getters and setters



Group_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Group.class)
public abstract class Group_
public static volatile SingularAttribute<Group, Integer> id;
public static volatile SingularAttribute<Group, String> title;










share|improve this question
















I'm using Hibernate 5.2.10 with dynamic criteria and find out that Fetch is not longer assignable to Join.



In the next example, I need to fetch group data in the same query, but I also need to use the group's field for sorting or restriction.



CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<GroupAssignment> query = cb.createQuery(GroupAssignment.class);

Root<GroupAssignment> root = query.from(GroupAssignment.class);
SingularAttributeJoin<GroupAssignment, Group> groupFetch = (SingularAttributeJoin<GroupAssignment, Group>) root.fetch(GroupAssignment_.group, JoinType.LEFT);
query.orderBy(cb.asc(groupFetch.get(Group_.title)));


I have manually cast Fetch to the SingularAttributeJoin and after that, I can use get method for the ordering purpose, but I'm looking for the right way how I can do that without casting manually.



GroupAssignment class:



@Entity()
@Table(name = "group_assignment")
public class GroupAssignment
@Id
@Column(name = "group_assignment_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "group_id", nullable = false)
private Group group;

//other fields, getters and setters



GroupAssignment_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(GroupAssignment.class)
public abstract class GroupAssignment_
public static volatile SingularAttribute<GroupAssignment, Integer> id;
public static volatile SingularAttribute<GroupAssignment, Group> group;



Group class:



@Entity()
@Table(name = "navigation_group")
public class Group
@Id
@Column(name = "group_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "title")
private String title;

//other fields, getters and setters



Group_ class:



@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Group.class)
public abstract class Group_
public static volatile SingularAttribute<Group, Integer> id;
public static volatile SingularAttribute<Group, String> title;







java hibernate jpa join fetch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:54









michaeak

779315




779315










asked Oct 2 '18 at 14:48









DeplakeDeplake

385419




385419












  • Can you put the code where you try to cast?

    – michaeak
    Nov 13 '18 at 12:20











  • It is already present in the original description (casting to the SingularAttributeJoin)

    – Deplake
    Nov 13 '18 at 12:22












  • Is this CriteriaBuilder the Hibernate-own CriteriaBuilder? Because there is a javax.persistence.criteria.CriteriaBuilder which is quite well documented

    – michaeak
    Nov 13 '18 at 12:26











  • Yes, it is javax.persistence.criteria.CriteriaBuilder

    – Deplake
    Nov 13 '18 at 12:30











  • Ok, good. Is it possible to post the Group, GroupAssignment, Group_ and GroupAssignment_ classes?

    – michaeak
    Nov 13 '18 at 12:36

















  • Can you put the code where you try to cast?

    – michaeak
    Nov 13 '18 at 12:20











  • It is already present in the original description (casting to the SingularAttributeJoin)

    – Deplake
    Nov 13 '18 at 12:22












  • Is this CriteriaBuilder the Hibernate-own CriteriaBuilder? Because there is a javax.persistence.criteria.CriteriaBuilder which is quite well documented

    – michaeak
    Nov 13 '18 at 12:26











  • Yes, it is javax.persistence.criteria.CriteriaBuilder

    – Deplake
    Nov 13 '18 at 12:30











  • Ok, good. Is it possible to post the Group, GroupAssignment, Group_ and GroupAssignment_ classes?

    – michaeak
    Nov 13 '18 at 12:36
















Can you put the code where you try to cast?

– michaeak
Nov 13 '18 at 12:20





Can you put the code where you try to cast?

– michaeak
Nov 13 '18 at 12:20













It is already present in the original description (casting to the SingularAttributeJoin)

– Deplake
Nov 13 '18 at 12:22






It is already present in the original description (casting to the SingularAttributeJoin)

– Deplake
Nov 13 '18 at 12:22














Is this CriteriaBuilder the Hibernate-own CriteriaBuilder? Because there is a javax.persistence.criteria.CriteriaBuilder which is quite well documented

– michaeak
Nov 13 '18 at 12:26





Is this CriteriaBuilder the Hibernate-own CriteriaBuilder? Because there is a javax.persistence.criteria.CriteriaBuilder which is quite well documented

– michaeak
Nov 13 '18 at 12:26













Yes, it is javax.persistence.criteria.CriteriaBuilder

– Deplake
Nov 13 '18 at 12:30





Yes, it is javax.persistence.criteria.CriteriaBuilder

– Deplake
Nov 13 '18 at 12:30













Ok, good. Is it possible to post the Group, GroupAssignment, Group_ and GroupAssignment_ classes?

– michaeak
Nov 13 '18 at 12:36





Ok, good. Is it possible to post the Group, GroupAssignment, Group_ and GroupAssignment_ classes?

– michaeak
Nov 13 '18 at 12:36












1 Answer
1






active

oldest

votes


















0














I think that javax.persistence.criteria.FetchParent may solve your problem:



FetchParent<GroupAssignment, Group> groupFetch = root.fetch(GroupAssignment_.group, JoinType.LEFT);





share|improve this answer


















  • 1





    I don't see how FetchParent can solve my issue. It is actually a superclass of Fetch, so it has even less public methods then Fetch...

    – Deplake
    Nov 14 '18 at 8:11











  • You are right. I had to do almost the same, using the FetchParent and manually casting to Join too, but later in the code, so I missed it when I first checked to see how I did. Sorry.

    – Sannon Aragão
    Nov 14 '18 at 13:40










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52610873%2fhibernate-fetch-is-not-longer-sub-type-of-join%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think that javax.persistence.criteria.FetchParent may solve your problem:



FetchParent<GroupAssignment, Group> groupFetch = root.fetch(GroupAssignment_.group, JoinType.LEFT);





share|improve this answer


















  • 1





    I don't see how FetchParent can solve my issue. It is actually a superclass of Fetch, so it has even less public methods then Fetch...

    – Deplake
    Nov 14 '18 at 8:11











  • You are right. I had to do almost the same, using the FetchParent and manually casting to Join too, but later in the code, so I missed it when I first checked to see how I did. Sorry.

    – Sannon Aragão
    Nov 14 '18 at 13:40















0














I think that javax.persistence.criteria.FetchParent may solve your problem:



FetchParent<GroupAssignment, Group> groupFetch = root.fetch(GroupAssignment_.group, JoinType.LEFT);





share|improve this answer


















  • 1





    I don't see how FetchParent can solve my issue. It is actually a superclass of Fetch, so it has even less public methods then Fetch...

    – Deplake
    Nov 14 '18 at 8:11











  • You are right. I had to do almost the same, using the FetchParent and manually casting to Join too, but later in the code, so I missed it when I first checked to see how I did. Sorry.

    – Sannon Aragão
    Nov 14 '18 at 13:40













0












0








0







I think that javax.persistence.criteria.FetchParent may solve your problem:



FetchParent<GroupAssignment, Group> groupFetch = root.fetch(GroupAssignment_.group, JoinType.LEFT);





share|improve this answer













I think that javax.persistence.criteria.FetchParent may solve your problem:



FetchParent<GroupAssignment, Group> groupFetch = root.fetch(GroupAssignment_.group, JoinType.LEFT);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 18:38









Sannon AragãoSannon Aragão

58112




58112







  • 1





    I don't see how FetchParent can solve my issue. It is actually a superclass of Fetch, so it has even less public methods then Fetch...

    – Deplake
    Nov 14 '18 at 8:11











  • You are right. I had to do almost the same, using the FetchParent and manually casting to Join too, but later in the code, so I missed it when I first checked to see how I did. Sorry.

    – Sannon Aragão
    Nov 14 '18 at 13:40












  • 1





    I don't see how FetchParent can solve my issue. It is actually a superclass of Fetch, so it has even less public methods then Fetch...

    – Deplake
    Nov 14 '18 at 8:11











  • You are right. I had to do almost the same, using the FetchParent and manually casting to Join too, but later in the code, so I missed it when I first checked to see how I did. Sorry.

    – Sannon Aragão
    Nov 14 '18 at 13:40







1




1





I don't see how FetchParent can solve my issue. It is actually a superclass of Fetch, so it has even less public methods then Fetch...

– Deplake
Nov 14 '18 at 8:11





I don't see how FetchParent can solve my issue. It is actually a superclass of Fetch, so it has even less public methods then Fetch...

– Deplake
Nov 14 '18 at 8:11













You are right. I had to do almost the same, using the FetchParent and manually casting to Join too, but later in the code, so I missed it when I first checked to see how I did. Sorry.

– Sannon Aragão
Nov 14 '18 at 13:40





You are right. I had to do almost the same, using the FetchParent and manually casting to Join too, but later in the code, so I missed it when I first checked to see how I did. Sorry.

– Sannon Aragão
Nov 14 '18 at 13:40



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52610873%2fhibernate-fetch-is-not-longer-sub-type-of-join%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20