Hibernate一对多映射错误

时间:2013-12-09 17:31:35

标签: spring hibernate one-to-many dao

我将开发医院管理系统。有一个如下的映射。

医生可以选择药片[selectedTablets] --->他们插入Eprescriber表单并保存为record.at开始有这些字段。患者姓名&为他选择了桌子

但是当我要开发它时会给我一个以下的错误。我将Mkyong Tutorials作为我的hibernate部分。

请帮我解决这个问题。 这是我的bean类。我的业务逻辑映射存在任何问题

@Entity
@Table(name = "E_PRESCRIBER")
public class EPrescriber implements Serializable {
    private static final long serialVersionUID = 440529869955257543L;
    public EPrescriber() {
        super();
    }

    public EPrescriber(int ePrescriberid, List<SelectedTablets> selectedTablets) {
        this.ePrescriberid = ePrescriberid;
        this.selectedTablets = selectedTablets;
    }

    @Id
    @Column(name = "ePrescriberid"  ,unique = true, nullable = false)
    @SequenceGenerator(name = "ePrescriber_seq", sequenceName = "ePrescriber_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ePrescriber_seq")
    private int ePrescriberid;

    public int getePrescriberid() {
        return ePrescriberid;
    }

    public void setePrescriberid(int ePrescriberid) {
        this.ePrescriberid = ePrescriberid;
    }

    @Column private String patientName;

    public String getPatientName() {
        return patientName;
    }


    public void setPatientName(String patientName) {
        this.patientName = patientName;
    }


    @OneToMany(fetch = FetchType.LAZY,targetEntity=SelectedTablets.class, mappedBy = "ePrescriberid")
    private List<SelectedTablets> selectedTablets=new ArrayList<SelectedTablets>();

    public List<SelectedTablets> getSelectedTablets() {
        return selectedTablets;
    }

    public void setSelectedTablets(List<SelectedTablets> selectedTablets) {
        this.selectedTablets = selectedTablets;
    }

}

这里的另一个bean类

@Entity
@Table(name = "SelectedTablets")
public class SelectedTablets implements Serializable {

    private static final long serialVersionUID = 4854785134773287611L;

    public SelectedTablets() {
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @SequenceGenerator(name = "selectedTablets_seq", sequenceName = "selectedTablets_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "selectedTablets_seq")
    private int id;

    @Column private Tablets tablets;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ePrescriberid", nullable = true)
    private EPrescriber ePrescriberid;

    public EPrescriber getePrescriberid() {
        return ePrescriberid;
    }

    public void setePrescriberid(EPrescriber ePrescriberid) {
        this.ePrescriberid = ePrescriberid;
    }

    public Tablets getTablets() {
        return tablets;
    }

    public void setTablets(Tablets tablets) {
        this.tablets = tablets;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

这是DAO类方法。看来这是我的问题。 ? ? ?

@Repository
@Transactional
public class EPrescriberDAO {

    @Autowired
    private SessionFactory sessionFactory;

public void getTabletbyNameAndSave(String []selectedMedicines) {
    EPrescriber ePrescriber=new EPrescriber();
    List<SelectedTablets> selectedTabletsList=new ArrayList<SelectedTablets>();
    for (String item : selectedMedicines) {
        Tablets tablets=null;   
        String hql="from Tablets t where t.category='"+item.trim()+"' ";
        Query queryList = sessionFactory.getCurrentSession().createQuery(hql);
        tablets=(Tablets)queryList.uniqueResult();

        SelectedTablets selectedTablets=new SelectedTablets();
        selectedTablets.setTablets(tablets);
        selectedTablets.setePrescriberid(ePrescriber);
        selectedTabletsList.add(selectedTablets);
        sessionFactory.getCurrentSession().save(selectedTablets);
    }
    ePrescriber.setPatientName("Ranil");
    ePrescriber.setSelectedTablets(selectedTabletsList);
    sessionFactory.getCurrentSession().save(ePrescriber);
}
}

如果我的DAO有问题,那么请告诉我正确的错误

-Thanks

错误日志

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.priyan.patients.EPrescriber
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
    at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
    at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:619)
    at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3141)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:501)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:227)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:150)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:997)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1142)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:835)
    at com.priyan.patients.EPrescriberDAO.getTabletbyNameAndSave(EPrescriberDAO.java:25)
    at com.priyan.patients.EPrescriberDAO$$FastClassByCGLIB$$59da35c6.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
    at com.priyan.patients.EPrescriberDAO$$EnhancerByCGLIB$$bb3659b.getTabletbyNameAndSave(<generated>)
    at com.priyan.patients.ContactsControllers.setTabletsNames(ContactsControllers.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1760)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "ePrescriberid", nullable = true)
private EPrescriber ePrescriberid;

SelectedTablets.java 如上编辑。现在我的问题排序&amp;谢谢4全部