0

I'm trying to make a simple join operation via jpa criteria api, but I'm getting an error:

java.lang.IllegalArgumentException: Unable to resolve attribute [Companies] against path at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120) at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:229) at org.hibernate.ejb.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:411) at com.maven_test.models.jpa.dao.ServicesDAO.findAllWithCompaniesByCriteria(ServicesDAO.java:106) at com.maven_test.models.jpa.ServicesFindByIdByCriteria.test2(ServicesFindByIdByCriteria.java:44) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Method looks like:

 public List findAllWithCompaniesByCriteria() { EntityManager em = EMgrUtil.createEntityManager(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Object[]> query = builder.createQuery(Object[].class); Metamodel m = em.getMetamodel(); EntityType<Services> Services_ = m.entity(Services.class); EntityType<Companies> Companies_ = m.entity(Companies.class); /*Root<Services> services = query.from(Services.class); Join<Services, Companies> companies = query.join(Companies_);*/ Root<Services> s = query.from(Services.class); Join<Services, Companies> c = s.join("Companies", JoinType.INNER); query.multiselect(s.get("avatar"), c.get("name")); List<Object[]> list = em.createQuery(query).getResultList(); return list; } 

A name of the table which we have to join is "zaks_companies", an error is same with both of cases.

@Entity @Table(name = "zaks_services") public class Services { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name = "title", nullable = false) @Size(min = 0, max = 255) private String title; //@Column(name = "descr", length = 65535, columnDefinition = "Text") //@Column(name = "descr", length = 4294967295, columnDefinition = "Longtext") @Column(name = "descr", nullable = false, length = 16777215, columnDefinition = "Mediumtext") private String descr; @Column(name = "avatar", nullable = false) private String avatar; @Column(name = "date_add", nullable = false, insertable = false, updatable = false, columnDefinition = "Datetime DEFAULT CURRENT_TIMESTAMP") @Temporal(TemporalType.TIMESTAMP) private Date date_add; @OneToOne @JoinColumn(name = "company_id") private Companies companies; ...getters/setters ...default constructor 

and Companies model:

@Entity @Table(name = "zaks_companies") public class Companies { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Integer id; @OneToOne(mappedBy = "companies") private Services services; @Column(name = "name", nullable = false) @Size(min = 0, max = 255) private String name; ...getters/setters ...default constructor 

One of my manuals was here: https://www.youtube.com/watch?v=J-f4jvljpgQ on 16:50

1
  • No one knows ?! Commented Mar 9, 2017 at 11:39

1 Answer 1

0

That's because names of the persistent attributes are case sensitive. In this case name of the persistent attribute is companies:

private Companies companies; 

but query tries to use Companies:

Join<Services, Companies> c = s.join("Companies", JoinType.INNER); 

In join one should use companies as follows:

Join<Services, Companies> c = s.join("companies", JoinType.INNER); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.