hbernate org.hibernate.persistentObject异常:未初始化的代理传递给org.hibernate.event.internal.DefaultSave()上的save()

hbernate org.hibernate.persistentObject异常:未初始化的代理传递给org.hibernate.event.internal.DefaultSave()上的save(),hibernate,hibernate-mapping,hibernate-criteria,Hibernate,Hibernate Mapping,Hibernate Criteria,这是hibernate dode我很担心请帮我解决这个问题 @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;

这是hibernate dode我很担心请帮我解决这个问题

@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

}
这是我的hibernate程序的dao部分

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");*/
}
}


com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306    /sehm5
根
根
真的
更新
我真的很困惑,无法找到解决方案,我到处搜索,但我没有找到解决方案。有人能告诉我哪里出了问题,我需要修改什么才能解决问题吗
提前谢谢

您可以仅更新受管实体。为了获取托管实体,您必须首先通过ID获取实体,将其连接到hibernate会话。在
更新(int primaryKey)
方法中,您应该向上移动代码:

redto=ses.get(retrainDto.class, new Integer(primarykey));
和地点,而不是

retrainDto redto=new retrainDto();
注意:如果您想创建新的实体,而不是更新现有实体,则必须手动为构造的实例分配ID属性(
redto.setCompanyId(sc.nextInt());
),因为数据库将为您执行此操作

retrainDto redto=new retrainDto();