我对hibernate有一点问题,因为hibernate不接受正常的sql查询语法。当我使用select语句发送我的查询时,该语句应该向数据库返回一个精确的整数37,而我却得不到任何回报。这是sql语法中的查询: “从tbl_employee中选择id,其中bsn ='36372837'”这将返回37.但是当我从hibernate中执行此查询时,对象引用和crap它不起作用。
请检查我的代码,看看你是否知道如何解决问题:
public void RegisterWorkHours(TimeRegistration object)
{
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("timereg");
EntityManager em = emf.createEntityManager();
try
{
String get_employee_id = "SELECT emp.id FROM Employee as emp WHERE emp.bsn=:bsn";
Query employee_query = em.createQuery(get_employee_id);
employee_query.setParameter("bsn", object.getEmployee().getBsn());
int id = employee_query.getFirstResult();
System.out.println("query returns employee id: " + id);
object.getEmployee().setId(id);
String get_project_id = "SELECT p.projectID FROM Project as p WHERE p.projectname=:projectname";
Query project_query = em.createQuery(get_project_id);
project_query.setParameter("projectname", object.getProject().getProjectname());
int projectid = project_query.getFirstResult();
System.out.println("query returns projectid: " + projectid);
object.getProject().setProjectID(projectid);
em.getTransaction().begin();
em.persist(object);
em.getTransaction().commit();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
员工类:
@Entity
@Table(name = "tbl_employee")
public class Employee
{
@Id
@SequenceGenerator(name="employeeSequence", sequenceName="SEQ_EMPLOYEE", allocationSize =1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="employeeSequence")
@Column(name="id")
private int id;
@Column(name = "bsn")
@NaturalId
private String bsn;
@Column(name = "first_name")
private String firstname;
@Column(name = "last_name")
private String lastname;
@Column(name="birth_date")
private String birthDate;
@Column(name="address")
private String address;
@Column(name="house_number")
private String houseNumber;
@Column(name="city")
private String city;
@Column(name="zip")
private String zip;
//Constructor
protected Employee() {}
public Employee(String bsn, String firstname, String lastname)
{
setBsn(bsn);
setFirstname(firstname);
setLastname(lastname);
}
public Employee(String bsn, String firstname, String lastname, String address, String housenumber)
{
setBsn(bsn);
setFirstname(firstname);
setLastname(lastname);
setAddress(address);
setHouseNumber(housenumber);
}
public Employee(String bsn, String firstname, String lastname, String address, String housenumber, String zip, String city)
{
setBsn(bsn);
setFirstname(firstname);
setLastname(lastname);
setAddress(address);
setHouseNumber(housenumber);
setZip(zip);
setCity(city);
}
//The rest is one big list of getters and setters.
}
TimeRegistration类
@Entity
@Table(name = "tbl_timeregtest")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class TimeRegistration
{
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Project project;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Employee employee;
@Id
@SequenceGenerator(name="timeregSequence", sequenceName="SEQ_TIMEREG", allocationSize =1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="timeregSequence")
@Column(name="ID")
private int ID;
@Column(name="date")
private String date;
@Column(name="hours")
private int hours;
//Constructor
protected TimeRegistration() {}
public TimeRegistration(Project project, Employee employee, String date, int hours )
{
setProject(project);
setEmployee(employee);
setDate(date);
setHours(hours);
}
//the rest is all getter setter stuff
}
主要空白
public class Main
{
public static void main(String [ ] args)
{
Persistence persistence = new Persistence();
Project project = new Project("AlphaMouse", "11-2-2013", "12-4-2019");
Employee employee = new Employee("398723912", "Stoel", "Stra");
TimeRegistration register = new TimeRegistration(project, employee, "21-2-2024", 8);
persistence.RegisterWorkHours(register) ;
}}
提前致谢, 本杰明
答案 0 :(得分:2)
您正在使用方法getFirstResult()
。
但是此方法返回表中记录的位置(整数)。
要检索记录(对象),您应该改为使用getSingleResult()
。
请参阅http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html。
答案 1 :(得分:0)
如果没有意义,请不要是JPA人员,请尝试将查询包装在交易中
em.getTransaction().begin();
String get_employee_id = "SELECT emp.id FROM Employee as emp WHERE emp.bsn=:bsn";
Query employee_query = em.createQuery(get_employee_id);
employee_query.setParameter("bsn", object.getEmployee().getBsn());
long id = employee_query.getFirstResult();
System.out.println("query returns employee id: " + id);
em.getTransaction().commit();
答案 2 :(得分:0)
我找到了解决方案,
Integer id = (Integer) em.createQuery("select id from Employee where bsn =:bsn")
.setParameter("bsn", object.getEmployee.getBsn())
.getSingleResult();
getSingleResult()返回一个对象,这就是为什么你必须使用Integer而不是int。