Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate createCriteria with uniqueResult多次执行选择查询_Hibernate_Jakarta Ee_Criteria_Hibernate Criteria - Fatal编程技术网

Hibernate createCriteria with uniqueResult多次执行选择查询

Hibernate createCriteria with uniqueResult多次执行选择查询,hibernate,jakarta-ee,criteria,hibernate-criteria,Hibernate,Jakarta Ee,Criteria,Hibernate Criteria,我使用createCriteria和uniqueResult按id选择对象,如下所示: Hibernate: select this_.employee_id as employee1_2_0_, this_.fk_department_id as fk13_2_0_, this_.email as email2_0_, this_.first_name as first5_2_0_, this_.last

我使用createCriteria和uniqueResult按id选择对象,如下所示:

Hibernate: 
    select
        this_.employee_id as employee1_2_0_,
        this_.fk_department_id as fk13_2_0_,
        this_.email as email2_0_,
        this_.first_name as first5_2_0_,
        this_.last_name as last8_2_0_,
        this_.password as password2_0_
    from
        employee this_ 
    where
        this_.employee_id = ?
Hibernate: 
    select
        this_.employee_id as employee1_2_0_,
        this_.fk_department_id as fk13_2_0_,
        this_.email as email2_0_,
        this_.first_name as first5_2_0_,
        this_.last_name as last8_2_0_,
        this_.password as password2_0_
    from
        employee this_ 
    where
        this_.employee_id = ?
Hibernate: 
    select
        this_.employee_id as employee1_2_0_,
        this_.fk_department_id as fk13_2_0_,
        this_.email as email2_0_,
        this_.first_name as first5_2_0_,
        this_.last_name as last8_2_0_,
        this_.password as password2_0_
    from
        employee this_ 
    where
        this_.employee_id = ?
1-服务:

    @Service
    @Transactional
    public class MyService {

    public Employee getEmployeeById(long employeeId) {
        return employeeDao.getEmployeeById(employeeId);      
       }

    }
  @Repository
  public class EmployeeDaoImpl extends AbstractDao implements EmployeeDao {

  public Employee getEmployeeById(long employeeId) {
    return (Employee) super.getById(Employee.class, employeeId);
     }

  }
@Repository
public class AbstractDao {

@Autowired
private SessionFactory sessionFactory;

public Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
   }

 public Object getById(Class clazz, long idValue) {
    return getCurrentSession().createCriteria(clazz)
            .add(Restrictions.idEq(idValue)).uniqueResult();
   }


}
@Entity
@Table(name = "employee")
public class Employee implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
private long id;

@Column(name = "first_name", length = 100, nullable = false)
private String firstName;

@Column(name = "last_name", length = 100, nullable = false)
private String lastName;

@Column(name = "email", length = 155, nullable = false)
private String email;

@Column(name = "password", nullable = false)
private String password;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "fk_department_id", nullable = true)
private Department department;

@ManyToMany(fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
@JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
private Set<Role> roles = new HashSet<Role>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.employee")
@Cascade(value = { CascadeType.ALL })
@Fetch(FetchMode.SELECT)
private Set<EmployeeGroup> employeeGroups = new HashSet<EmployeeGroup>(0);

}
2-DAO:

    @Service
    @Transactional
    public class MyService {

    public Employee getEmployeeById(long employeeId) {
        return employeeDao.getEmployeeById(employeeId);      
       }

    }
  @Repository
  public class EmployeeDaoImpl extends AbstractDao implements EmployeeDao {

  public Employee getEmployeeById(long employeeId) {
    return (Employee) super.getById(Employee.class, employeeId);
     }

  }
@Repository
public class AbstractDao {

@Autowired
private SessionFactory sessionFactory;

public Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
   }

 public Object getById(Class clazz, long idValue) {
    return getCurrentSession().createCriteria(clazz)
            .add(Restrictions.idEq(idValue)).uniqueResult();
   }


}
@Entity
@Table(name = "employee")
public class Employee implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
private long id;

@Column(name = "first_name", length = 100, nullable = false)
private String firstName;

@Column(name = "last_name", length = 100, nullable = false)
private String lastName;

@Column(name = "email", length = 155, nullable = false)
private String email;

@Column(name = "password", nullable = false)
private String password;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "fk_department_id", nullable = true)
private Department department;

@ManyToMany(fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
@JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
private Set<Role> roles = new HashSet<Role>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.employee")
@Cascade(value = { CascadeType.ALL })
@Fetch(FetchMode.SELECT)
private Set<EmployeeGroup> employeeGroups = new HashSet<EmployeeGroup>(0);

}
3-抽象道:

    @Service
    @Transactional
    public class MyService {

    public Employee getEmployeeById(long employeeId) {
        return employeeDao.getEmployeeById(employeeId);      
       }

    }
  @Repository
  public class EmployeeDaoImpl extends AbstractDao implements EmployeeDao {

  public Employee getEmployeeById(long employeeId) {
    return (Employee) super.getById(Employee.class, employeeId);
     }

  }
@Repository
public class AbstractDao {

@Autowired
private SessionFactory sessionFactory;

public Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
   }

 public Object getById(Class clazz, long idValue) {
    return getCurrentSession().createCriteria(clazz)
            .add(Restrictions.idEq(idValue)).uniqueResult();
   }


}
@Entity
@Table(name = "employee")
public class Employee implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
private long id;

@Column(name = "first_name", length = 100, nullable = false)
private String firstName;

@Column(name = "last_name", length = 100, nullable = false)
private String lastName;

@Column(name = "email", length = 155, nullable = false)
private String email;

@Column(name = "password", nullable = false)
private String password;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "fk_department_id", nullable = true)
private Department department;

@ManyToMany(fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
@JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
private Set<Role> roles = new HashSet<Role>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.employee")
@Cascade(value = { CascadeType.ALL })
@Fetch(FetchMode.SELECT)
private Set<EmployeeGroup> employeeGroups = new HashSet<EmployeeGroup>(0);

}
4-实体:

    @Service
    @Transactional
    public class MyService {

    public Employee getEmployeeById(long employeeId) {
        return employeeDao.getEmployeeById(employeeId);      
       }

    }
  @Repository
  public class EmployeeDaoImpl extends AbstractDao implements EmployeeDao {

  public Employee getEmployeeById(long employeeId) {
    return (Employee) super.getById(Employee.class, employeeId);
     }

  }
@Repository
public class AbstractDao {

@Autowired
private SessionFactory sessionFactory;

public Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
   }

 public Object getById(Class clazz, long idValue) {
    return getCurrentSession().createCriteria(clazz)
            .add(Restrictions.idEq(idValue)).uniqueResult();
   }


}
@Entity
@Table(name = "employee")
public class Employee implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
private long id;

@Column(name = "first_name", length = 100, nullable = false)
private String firstName;

@Column(name = "last_name", length = 100, nullable = false)
private String lastName;

@Column(name = "email", length = 155, nullable = false)
private String email;

@Column(name = "password", nullable = false)
private String password;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "fk_department_id", nullable = true)
private Department department;

@ManyToMany(fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
@JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
private Set<Role> roles = new HashSet<Role>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.employee")
@Cascade(value = { CascadeType.ALL })
@Fetch(FetchMode.SELECT)
private Set<EmployeeGroup> employeeGroups = new HashSet<EmployeeGroup>(0);

}

你知道我为什么会有这样的行为以及如何调整吗?

为什么首先要使用createcriteria?如果您已经知道实体的id,那么对Session.load(Class,id)的简单调用要简单得多。()


您能创建一个简单的测试方法并将其源代码与运行日志一起发布吗?

首先为什么要使用createcriteria?如果您已经知道实体的id,那么对Session.load(Class,id)的简单调用要简单得多。()

您能否创建一个简单的测试方法,并将其源代码与运行日志一起发布