休眠选择查询不返回任何内容
Posted
技术标签:
【中文标题】休眠选择查询不返回任何内容【英文标题】:Hibernate select query returns nothing 【发布时间】:2012-02-23 15:15:37 【问题描述】:我在使用 hibernate 时遇到了一点问题,因为 hibernate 不接受正常的 sql 查询语法。当我使用 select 语句发送查询时,该语句应该将 37 的精确整数返回到数据库,但我没有得到任何回报。这是 sql 语法中的查询: "select id from tbl_employee where bsn = '36372837'" 这将返回 37。但是当我在 hibernate 中使用所有对象引用执行此查询时,它不起作用。
请检查我的代码,看看你是否知道如何解决问题:
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.
时间注册类
@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) ;
提前致谢, 本杰明
【问题讨论】:
可能需要查看您的 Employee 对象和您设置的映射(或注解) oke Kevin Crowell 我添加了 Employee 和 TimeRegistration 类,我还添加了对象的主要 void。 【参考方案1】:您正在使用方法getFirstResult()
。
但是这个方法返回记录在表中的位置(整数)。
要检索记录(对象),您应该改用getSingleResult()
。
见http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html。
【讨论】:
【参考方案2】:我不是 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();
【讨论】:
感谢@Sajan 的反馈,这可能是有道理的,但它不起作用:(...我希望其他人可以帮助我。 @B Westra 尝试在查询中的等号和“:”之间添加空格。 "SELECT emp.id FROM Employee as emp WHERE emp.bsn = :bsn"; 我试过了,但没用。它仍然返回 0 而应该返回 37。【参考方案3】:我找到了解决办法,
Integer id = (Integer) em.createQuery("select id from Employee where bsn =:bsn")
.setParameter("bsn", object.getEmployee.getBsn())
.getSingleResult();
getSingleResult() 返回一个对象,所以你必须使用 Integer 而不是 int。
【讨论】:
以上是关于休眠选择查询不返回任何内容的主要内容,如果未能解决你的问题,请参考以下文章