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

时间:2018-09-27 19:48:38

标签: hibernate hibernate-mapping hibernate-criteria

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

g.V().hasLabel("my-label").toList()

这是我的休眠程序的重要部分

@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

}

}

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

我真的很困惑,无法为此找到解决方案,我到处搜索了bt,但我没有解决方案,任何人都可以清除我出了错的地方以及需要修改的内容才能工作 预先感谢!

1 个答案:

答案 0 :(得分:1)

您只能更新受管理实体。为了获得托管实体,您必须首先通过按ID提取实体将其附加到休眠会话。在您的update(int primaryKey)方法中,您应该将代码上移:

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

并放置而不是

retrainDto redto=new retrainDto();

P.S。如果要创建新实体而不是更新现有实体,则必须手动为已构造实例分配ID属性(redto.setCompanyId(sc.nextInt());),因为数据库将为你做