java - 从两个类内部加入 hibernate

标签 java mysql hibernate join inner-join

我有这两个域类: 员工.java

package it.jack.fdd.domain;
// Generated 9-dic-2016 17.38.23 by Hibernate Tools 4.3.1.Final

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.codehaus.jackson.annotate.JsonIgnore;

/**
 * Staff generated by hbm2java
 */
@Entity
@Table(name = "staff", catalog = "fdd_dbproducts")
public class Staff implements java.io.Serializable {

	private Integer idstaff;
	private StaffType staffType;
	private String name;
	private String surname;
	private Date birthDate;
	private String phone;
	private boolean gender;
	private Boolean working;
	private StaffLogin staffLogin;
	private Set<RtStaffDispenser> rtStaffDispensers = new HashSet<RtStaffDispenser>(0);

	public Staff() {
	}

	public Staff(StaffType staffType, String name, String surname, Date birthDate, String phone, boolean gender) {
		this.staffType = staffType;
		this.name = name;
		this.surname = surname;
		this.birthDate = birthDate;
		this.phone = phone;
		this.gender = gender;
	}

	public Staff(StaffType staffType, String name, String surname, Date birthDate, String phone, boolean gender,
			Boolean working, StaffLogin staffLogin, Set<RtStaffDispenser> rtStaffDispensers) {
		this.staffType = staffType;
		this.name = name;
		this.surname = surname;
		this.birthDate = birthDate;
		this.phone = phone;
		this.gender = gender;
		this.working = working;
		this.staffLogin = staffLogin;
		this.rtStaffDispensers = rtStaffDispensers;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)

	@Column(name = "idstaff", unique = true, nullable = false)
	public Integer getIdstaff() {
		return this.idstaff;
	}

	public void setIdstaff(Integer idstaff) {
		this.idstaff = idstaff;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	//metto EAGER al posto di LAZY altrimenti mi da errore 500 e per quanto riguarda la chiave esterna.
	@JoinColumn(name = "fkstaff_type_staff", nullable = false)
	public StaffType getStaffType() {
		return this.staffType;
	}

	public void setStaffType(StaffType staffType) {
		this.staffType = staffType;
	}

	@Column(name = "name", nullable = false, length = 45)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "surname", nullable = false, length = 45)
	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "birth_date", nullable = false, length = 0)
	public Date getBirthDate() {
		return this.birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	@Column(name = "phone", nullable = false, length = 45)
	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(name = "gender", nullable = false)
	public boolean isGender() {
		return this.gender;
	}

	public void setGender(boolean gender) {
		this.gender = gender;
	}

	@Column(name = "working")
	public Boolean getWorking() {
		return this.working;
	}

	public void setWorking(Boolean working) {
		this.working = working;
	}

	@OneToOne(fetch = FetchType.EAGER, mappedBy = "staff")
	public StaffLogin getStaffLogin() {
		return this.staffLogin;
	}

	public void setStaffLogin(StaffLogin staffLogin) {
		this.staffLogin = staffLogin;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "staff")
	@JsonIgnore
	public Set<RtStaffDispenser> getRtStaffDispensers() {
		return this.rtStaffDispensers;
	}

	public void setRtStaffDispensers(Set<RtStaffDispenser> rtStaffDispensers) {
		this.rtStaffDispensers = rtStaffDispensers;
	}

}

RtStaffDispenser.java

package it.jack.fdd.domain;
// Generated 30-nov-2016 0.17.09 by Hibernate Tools 4.3.1.Final

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * RtStaffDispenser generated by hbm2java
 */
@Entity
@Table(name = "rt_staff_dispenser", catalog = "fdd_dbproducts")
public class RtStaffDispenser implements java.io.Serializable {

	private Integer idrtStaffDispenser;
	private Dispenser dispenser;
	private Staff staff;

	public RtStaffDispenser() {
	}

	public RtStaffDispenser(Dispenser dispenser, Staff staff) {
		this.dispenser = dispenser;
		this.staff = staff;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)

	@Column(name = "idrt_staff_dispenser", unique = true, nullable = false)
	public Integer getIdrtStaffDispenser() {
		return this.idrtStaffDispenser;
	}

	public void setIdrtStaffDispenser(Integer idrtStaffDispenser) {
		this.idrtStaffDispenser = idrtStaffDispenser;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "fkdispenser_rtsdispenser", nullable = false)
	public Dispenser getDispenser() {
		return this.dispenser;
	}

	public void setDispenser(Dispenser dispenser) {
		this.dispenser = dispenser;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "fkstaff_rtsdispenser", nullable = false)
	public Staff getStaff() {
		return this.staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

}

谁在数据库中有此关系:

enter image description here

我想在 hibernate 中进行查询,显示 Staff INNER JOIN RtStaffDispenser ON Staff.idstaff = RtStaffDispenser.idRtsdispenser 的所有元素。 我怎样才能做到呢?我知道SQL INNER JOIN,但我不知道HIBERNATE INNER JOIN

JUNIT 错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [from it.jack.fdd.domain.Staff inner join RtStaffDispenser on Staff.idstaff = RtStaffDispenser.idrtStaffDispenser]
	at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
	at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:284)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
	at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
	at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
	at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
	at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
	at it.jack.fdd.dao.impl.StaffDaoImpl.getAllA(StaffDaoImpl.java:95)
	at it.jack.fdd.tests.StaffDaoImplTest.testGetAllA(StaffDaoImplTest.java:62)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

最佳答案

这样解决:

“从 RtStaffDispenser r 内连接 r.staff s 中选择 s”

关于java - 从两个类内部加入 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41546891/

相关文章:

java - 解决java中的空字符串

java - Spring @Value 与多矿属性文件值

php - 从一张表迁移到另一张表

jpa 查询不会触发 hibernate envers

java - Hibernate创建表但不设置对象

java - Eclipse PDE : How to load a new project in Package Explorer programmatically?

java - 将Cucumber场景DataTable(HashMap)转换为方法调用

MySql 索引填充因子?

php - 如何更改 !isset 语句

java - hibernate 环境 : modified field flag name