且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

休眠,会话,延迟加载

更新时间:2022-10-24 08:31:45

正如在另一个答案中所说的,你应该是提交你已经开始的交易。您在阅读数据时不需要交易。


问题1:延迟加载。


blockquote>

您需要延迟加载 项目,这样它可以在你的视图中***使用。要做到这一点,请修改下面的代码,

  @OneToMany(fetch = FetchType.LAZY)
private List<项目&GT;项目;




问题2:具有相同标识符的对象已经存在。 p>

调试并找出要保存的对象的 id


问题3:session和sessionfactory。


以下是两者之间的区别:文章


SessionFactory是单一数据存储的Hibernates概念,并且是线程安全的,因此许多线程都可以访问它同时为单个数据库请求会话和编译映射的不可变缓存。 SessionFactory通常只在启动时创建一次。 SessionFactory应该用某种单例包装,以便在应用程序代码中轻松访问它。

会话是一个轻量级和非线程安全的对象(不能在线程之间共享),它代表了与数据库的单个工作单元。会话由SessionFactory打开,然后在所有工作完成时关闭。会话是持久性服务的主要接口。会话会延迟地获得数据库连接(即仅在需要时)。为避免创建过多会话,无论您调用currentSession()方法多少次,都可以使用ThreadLocal类来获取当前会话。


因此,在你的代码中,你应该为 session 创建一个局部变量,并且每次关闭该方法时关闭它。


I am developing a Java web application using Hibernate and JSF/primefaces. am sometimes getting errors like

1) an object with same identifier is already associated with session.

2) failed to load lazy initialization *,no session exist or session is already closed.

I know this is due to improper coding in my app. this is the way am doing aap:

When a user requests for a page(Let it be a list of Employees). the user will get the employee list page(empployeeList.xhtml) EmployeeListMBean is the managed bean for this page. in the managed bean in the constructor, am calling a method populateEmployees(). populateEmployee() will use the EmployeeDao method getAllEmployee() to getAllemployees.

Employee Class goes here:

@Entity
@Table(name = "Employee")
@NamedQueries({
    @NamedQuery(name = "Employee.getAllEmployee", query = "from Employee"),
    @NamedQuery(name = "Employee.findEmployeeByFirstName", query = "from Employee where firstName = :firstName"),
    @NamedQuery(name = "Employee.findEmployeeByLastName", query = "from Employee where lastName = :lastName"),
    @NamedQuery(name = "Employee.findEmployeeByMiddleName", query = "from Employee where middleName = :middleName"),
    @NamedQuery(name = "Employee.findEmployeeByOffice", query = "from Employee where office.id = :officeId")
})
public class Employee implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "EID")
    private long id;
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Column(name = "LAST_NAME")
    private String lastName;
    @Column(name = "GENDER")
    private String gender;
    @Column(name = "DOB")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateOfBirth;
    @Column(name = "DOH")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateOfHire;
    @ManyToOne(cascade= CascadeType.ALL)
    private Office office;
    @OneToOne(cascade = CascadeType.ALL)
    private ResidenceAddress residence;
    @OneToMany
    private List<Project> projects;

    //getters and setters

}

here is my EmployeeDao:

public class EmployeeDao implements Serializable{
    private SessionFactory factory;
    private Session session;
    public void addEmployee(Employee employee){
        factory = HibernateUtil.getSessionFactory();
        session = factory.openSession();
        session.beginTransaction();
        session.save(employee); 
        session.getTransaction.commit();        
    }
    public List<Employee> getAllEmployee(){
        factory = HibernateUtil.getSessionFactory();
        session = factory.openSession();
        session.beginTransaction();
        List<Employee> cities = session.getNamedQuery("Employee.getAllEmployee").list();
        session.close();
        return cities;
    }

    public Employee getEmployeeByEmployeeId(long employeeId){
        factory = HibernateUtil.getSessionFactory();
        session = factory.openSession();
        session.beginTransaction();
        Employee employee = (Employee) session.get(Employee.class, employeeId);
        session.close();
        return employee;
    }    
}

Question 1) here, in the methods I am closing the session and then return back the result to managedbeans. so in the employee listing page the table list out name dob dateOfHire. and I have a buutton view more detail. on clicking this button, I want to display all projects of selected employee working on using the same managedbeans but, it is giving me error(2), failed to lazyload, no session or session already closed. if I keep the session opened in the getemployeeMethod of dao, I guess that may lead to a memory leak problem, or someother problem. is it so? also, I have tried lazy and eager loading. please give me a clear idea when/how to use these type of fetching. How can I solve this? can I go for filters or facelisteners for solving this?

Question 2) if am trying to edit a project of an employee, and update using session.saveorupadte(), merge(),flush(), am getting an error like this, "an object with same identifier is already associated with session" How can I solve this?

Question 3) I know that sessionfactory is resource consuming. so only single instance is enough for one app. but what about session? for a SINGLE USER of app, only one session is needed? please tell me good strategy for developing such an app.

Thanking you all :)

As told in the other answer, you should be commiting the transactions you have started. The Transactions are not needed while you are reading the data.

Question 1 : Lazy Loading.

You need to lazy load projects, so that it can be used freely in your view. To do that, modify the code as below,

@OneToMany(fetch=FetchType.LAZY)
private List<Project> projects;

Question 2 : an object with same identifier is already exist.

Debug and find out what is the id of the object you are going to save. I can't much help here without a stacktrace of your error.

Question 3 : session and sessionfactory.

Here is the difference between the two from this article.

SessionFactory is Hibernates concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. A SessionFactory is usually only built once at startup. SessionFactory should be wrapped in some kind of singleton so that it can be easily accessed in an application code.

Session is a light weight and a non-threadsafe object (No, you cannot share it between threads) that represents a single unit-of-work with the database. Sessions are opened by a SessionFactory and then are closed when all work is complete. Session is the primary interface for the persistence service. A session obtains a database connection lazily (i.e. only when required). To avoid creating too many sessions ThreadLocal class can be used as shown below to get the current session no matter how many times you make call to the currentSession() method.

So, in your code you should create a local variable for session and close it every time you close the method.