我正在尝试通过jpa标准api进行简单的连接操作,但我收到错误:
java.lang.IllegalArgumentException:无法针对路径解析属性[Companies] at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120) 在org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:229) 在org.hibernate.ejb.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:411) 在com.maven_test.models.jpa.dao.ServicesDAO.findAllWithCompaniesByCriteria(ServicesDAO.java:106) 在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) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在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) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
方法如下:
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;
}
我们必须加入的表名是“zaks_companies”,两种情况的错误相同。
@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
和公司模特:
@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
我的一本手册在这里:https://www.youtube.com/watch?v=J-f4jvljpgQ于16:50
答案 0 :(得分:0)
那是因为持久属性的名称区分大小写。在这种情况下,持久属性的名称为companies
:
private Companies companies;
但查询尝试使用Companies
:
Join<Services, Companies> c = s.join("Companies", JoinType.INNER);
在加入时,应使用companies
,如下所示:
Join<Services, Companies> c = s.join("companies", JoinType.INNER);