简体   繁体   English

通用DAO Hibernate Java

[英]Generic DAO Hibernate Java

I'm trying to understand Generic DAO's at their most basic level. 我试图从最基本的角度理解通用DAO。 I've got the basic CRUD methods created in the DAO Here: 我已经在DAO中创建了基本的CRUD方法:

public interface ObjectDAO <T>{

boolean insert(T t);

boolean update(int id, T t);



boolean delete(int id);

T retrieveByID(int id);

List<T> retrieveAll();



}

However, I cannot figure out how to get the id for the class that I pass into the ObjectDAOImpl. 但是,我不知道如何获取传递给ObjectDAOImpl的类的ID。 Here is the implementation: 这是实现:

public class ImplObjectDAO<T> implements ObjectDAO<T>{

Session session;

public ImplObjectDAO() {

    session= SessionSingleton.getInstance();
}

@Override
public boolean insert(T t){

        try{

        session.beginTransaction();

        session.save(t);

        session.getTransaction().commit();

        }
        catch(HibernateException he){

            he.printStackTrace();
            return false;
    }   
    return true;
}


@Override
public boolean update(int id, T t) {

    try{

        session.beginTransaction();




        session.update(t);

        session.getTransaction().commit();

        return true;
        }
        catch(HibernateException he){
            he.printStackTrace();
            return false;
        }
        catch(NullPointerException np){
            np.printStackTrace();
            return false;
        }
}


public boolean delete(int id){



    return true;

}

@Override
public <T> retrieveByID(int id){

    return T;
}

@Override
public List<T> retrieveAll(){
    return new ArrayList<T>(objectSet);

}


}

So if i pass an object like the employee class that I've created. 因此,如果我传递我创建的类似于雇员类的对象。 The insert method works fine. 插入方法工作正常。 But once it's been inserted into the database, how do I get the id of the Entity so that I can do things like update, or delete, or retrieve? 但是,一旦将其插入数据库中,我如何获取实体的ID,以便可以执行诸如更新,删除或检索之类的操作? I am not understanding. 我不理解。 Any help is appreciated. 任何帮助表示赞赏。

Here is the entity class: 这是实体类:

@Entity
@Table(name="Employee_Records")
public class Employee {

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private int eID;

@Column(name="EMP_NAME", length=20)
private String empName;

@Column(name="EMPLOYEE_AGE")
private int empAge;

public Employee(){
    eID= 0;
    empName="temp";
    empAge=0;
}

public Employee(int eID, String empName, int empAge) {
    this.eID = eID;
    this.empName = empName;
    this.empAge = empAge;
}

public int geteID() {
    return eID;
}

public void seteID(int eID) {
    this.eID = eID;
}

public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

public int getEmpAge() {
    return empAge;
}

public void setEmpAge(int empAge) {
    this.empAge = empAge;
}



@Override
public String toString() {
    return "Employee ID=" + eID + ", Employee Name=" + empName + ", Employee    Age="
            + empAge;
}
}

Well, If you want to retrieve the id of the object you just inserted. 好吧,如果要检索刚插入的对象的ID。 You can actually get the return value of the save method you just called. 实际上,您可以获取刚刚调用的save方法的返回值。

So, in the example code, instead of return true when you save the object, you can return the id of the object instead. 因此,在示例代码中,您可以返回对象的ID,而不是在save对象时返回true。

you can check the example here . 您可以在此处查看示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM