Reputation: 39
this is the hibernate code im getting worried about please help me to solve this
@Entity
@Table(name="nse_table")
public class retrainDto implements Serializable{
@Id
@Column(name="company_Id")
private int companyId;
@Column(name="company_symbol")
private String companySymbol;
// some other columns omitted
}
this is the dao part of my hibernate program
public class retrainDao {
public Scanner sc=new Scanner(System.in);
Configuration cfg=new Configuration().configure();
SessionFactory sf=cfg.buildSessionFactory();
Session ses=sf.openSession();
Transaction tx=ses.beginTransaction();
public void saveRetrain(retrainDto dto) {
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session ses=sf.openSession();
Transaction tx=ses.beginTransaction();
retrainDto redto=new retrainDto();
ses.save(dto);
tx.commit();
}
public retrainDto update(int primarykey) {
Session ses=null;
Transaction tx=null;
try {
ses=sf.openSession();
tx=ses.beginTransaction();
retrainDto redto=new retrainDto();
System.out.println("give primary key");
System.out.println("give company id :");
redto.setCompanyId(sc.nextInt());
System.out.println("give company symbol");
redto.setCompanySymbol(sc.next());
// set other columns
redto=ses.load(retrainDto.class, new Integer(primarykey));
System.out.println("data updated succesfully");
ses.save(redto);
tx.commit();
}catch(HibernateException e) {e.printStackTrace();}finally {if(ses!=null)ses.close();}
return null;
}
public retrainDto readData(int primarykey) {
Session ses=null;
Transaction tx=null;
retrainDto redto=null;
try {
ses=sf.openSession();
tx=ses.beginTransaction();
System.out.println("confirm data want to retrieve");
redto=ses.get(retrainDto.class,sc.nextInt());
System.out.println(redto);
tx.commit();
}catch(HibernateException e) {
e.printStackTrace();}finally{if(ses!=null)ses.close();}
return null;
}
public retrainDto readbyhql(String name) {
Session ses=null;
Transaction tx=null;
retrainDto redto=null;
try {
ses=sf.openSession();
tx=ses.beginTransaction();
System.out.println("try part");
String hql="FROM retrainDto WHERE companySymbol=?";
Query query=ses.createQuery(hql,retrainDto.class);
query.setParameter(0, name);
redto=(retrainDto)((org.hibernate.query.Query)query).uniqueResult();
System.out.println("fetching data");
System.out.println(redto);
tx.commit();
}catch(HibernateException e) {
e.printStackTrace();
}finally{if (ses!=null)ses.close();}
return redto;
}
}
public class retrainTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
retrainDto dto=new retrainDto();
/*System.out.println("give id :");
dto.setRetrainId(sc.nextInt());
System.out.println("give name :");
dto.setRetrainName(sc.next());
System.out.println("give percentage :");
dto.setRetrainPercentage(sc.nextInt());
System.out.println("created successfully");*/
retrainDao dao=new retrainDao();
/*dao.saveRetrain(dto);
System.out.println("saved successfully");*/
System.out.println("update id please");
dao.update(sc.nextInt());
/*System.out.println("give data to read :");
dao.readData(sc.nextInt());*/
/*System.out.println("sql by reading is started please enter the symbol :");
dao.readbyhql(sc.next());
System.out.println("completed reading");*/
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306 /sehm5</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.application.retrain.dto.retrainDto"></mapping>
</session-factory>
</hibernate-configuration>
I really so confused to get solution for this i searched everywhere bt i didnt got solution can any one please clear me where i went wrong and what i have to modify to work it thanks in advance!
Upvotes: 1
Views: 2401
Reputation: 7521
You can update only managed entities. In order to get managed entity you must first attach it to hibernate session by fetching entity by ID. In your update(int primaryKey)
method you should move your code up:
redto=ses.get(retrainDto.class, new Integer(primarykey));
and place instead of
retrainDto redto=new retrainDto();
P.S. if you want to create new entity instead of updating existing one, you must not manually assign ID property (redto.setCompanyId(sc.nextInt());
) for a constructed instance, because the Database will do it for you
Upvotes: 1