hibernate org.hibernate.PersistentObjectException:未初始化的代理传递给 org.hibernate.event.internal.DefaultSave 处的 save()

问题描述 投票:0回答:1

这是我担心的休眠代码,请帮我解决这个问题

@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
    
}
    

这是我的休眠程序的 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");*/
}

}

<?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>

我真的很困惑要找到这个问题的解决方案,我到处搜索,但我没有找到解决方案,任何人都可以请清除我哪里出错了,以及我必须修改什么才能工作 预先感谢!

hibernate hibernate-mapping hibernate-criteria
1个回答
1
投票

您只能更新托管实体。为了获取托管实体,您必须首先通过 ID 获取实体将其附加到休眠会话。在您的

update(int primaryKey)
方法中,您应该向上移动代码:

redto=ses.get(retrainDto.class, new Integer(primarykey));

并放置而不是

retrainDto redto=new retrainDto();

附注如果您想创建新实体而不是更新现有实体,则必须手动为构造的实例分配ID属性(

redto.setCompanyId(sc.nextInt());
),因为数据库会为您完成这件事

© www.soinside.com 2019 - 2024. All rights reserved.