我正在尝试通过 jpa 标准 api 进行简单的连接操作,但出现错误:
java.lang.IllegalArgumentException:无法针对 org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120) 在 org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute 的路径解析属性 [Companies] (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) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect。 org.junit 上的 DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)。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) 在 org.junit.runners 的 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 的 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)。 BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit。 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 上 org.junit.runners 上的 runners.ParentRunner$1.schedule(ParentRunner.java:71)。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) 在 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) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)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) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) 在太阳。 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 处的 reflect.NativeMethodAccessorImpl.invoke0(Native Method)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) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) 在太阳。 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 处的 reflect.NativeMethodAccessorImpl.invoke0(Native Method)startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)execution.application.AppMain.main(AppMain.java:134)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