Spring Hibernate get tuple by id -
i trying create simple "teacher" database. able create method adding new teacher database, need able 1 , delete 1 "id". help, of how should implement it. have added following code files: appconfig, teacher (entity class), iteacherdao (interface), teacherdao(implementation of interface) , class main method timestarapplication.
appconfig
package com.superum.timestar; import javax.sql.datasource; import org.apache.commons.dbcp.basicdatasource; import org.hibernate.sessionfactory; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.orm.hibernate4.hibernatetemplate; import org.springframework.orm.hibernate4.hibernatetransactionmanager; import org.springframework.orm.hibernate4.localsessionfactorybuilder; import org.springframework.transaction.annotation.enabletransactionmanagement; import com.superum.timestar.dao.iteacherdao; import com.superum.timestar.dao.teacherdao; import com.superum.timestar.entity.teacher; @configuration @enabletransactionmanagement public class appconfig { @bean public iteacherdao teacherdao() { return new teacherdao(); } @bean public hibernatetemplate hibernatetemplate() { return new hibernatetemplate(sessionfactory()); } @bean public sessionfactory sessionfactory() { return new localsessionfactorybuilder(getdatasource()) .addannotatedclasses(teacher.class) .buildsessionfactory(); } @bean public datasource getdatasource() { basicdatasource datasource = new basicdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://localhost:3306/time_star"); datasource.setusername("root"); datasource.setpassword("password"); return datasource; } @bean public hibernatetransactionmanager hibtransman(){ return new hibernatetransactionmanager(sessionfactory()); } }
teacher
package com.superum.timestar.entity; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.table; @entity @table(name="teachers") public class teacher { @id@generatedvalue @column(name="t_id") private int id; @column(name="t_name") private string name; @column(name="t_lastname") private string lastname; @column(name="t_phone") private string phone; @column(name="t_age") private int age; public int getid() { return id; } public void setid(int id) { id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public int getage() { return age; } public void setage(int age) { this.age = age; }
}
iteacherdao
package com.superum.timestar.dao; public interface iteacherdao { public void addteacher(string name, string lastname, string phone, int age); // public void getteacher(int id); `` // public void deleteteacher(); }
teacherdao
package com.superum.timestar.dao; import javax.transaction.transactional; import org.springframework.beans.factory.annotation.autowired; import org.springframework.orm.hibernate4.hibernatetemplate; import com.superum.timestar.entity.teacher; @transactional public class teacherdao implements iteacherdao{ @autowired private hibernatetemplate hibernatetemplate; public void addteacher(string name, string lastname, string phone, int age){ teacher teacher = new teacher(); teacher.setname(name); teacher.setlastname(lastname); teacher.setphone(phone); teacher.setage(age); hibernatetemplate.save(teacher); } // need create id // public void getteacher(int id){ // // }
i hope question not entirely horrible.
}
iteacherdao
package com.superum.timestar.dao; public interface iteacherdao { public void addteacher(string name, string lastname, string phone, int age); public teacher getteacher(int id); //method teacher id public void deleteteacher(int id); //method delete teacher id }
teacherdao
package com.superum.timestar.dao; import javax.transaction.transactional; import org.springframework.beans.factory.annotation.autowired; import org.springframework.orm.hibernate4.hibernatetemplate; import com.superum.timestar.entity.teacher; @transactional public class teacherdao implements iteacherdao{ @autowired private hibernatetemplate hibernatetemplate; public void addteacher(string name, string lastname, string phone, int age){ teacher teacher = new teacher(); teacher.setname(name); teacher.setlastname(lastname); teacher.setphone(phone); teacher.setage(age); hibernatetemplate.save(teacher); } public teacher getteacher(int id){ teacher teacher = (teacher)hibernatetemplate.get(teacher.class, id); return teacher; } public void deleteteacher(int id){ teacher teacher = (teacher)hibernatetemplate.get(teacher.class, id); hibernatetemplate.delete(teacher); }
Comments
Post a Comment