简体   繁体   中英

Generic DAO Hibernate Java

I'm trying to understand Generic DAO's at their most basic level. I've got the basic CRUD methods created in the DAO Here:

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. 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? 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. You can actually get the return value of the save method you just called.

So, in the example code, instead of return true when you save the object, you can return the id of the object instead.

you can check the example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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