2013-12-09 550 views
-1

我打算開發醫院管理系統,因爲有像下面這樣的映射。休眠一對多映射錯誤

醫生可以選擇片劑[selectedTablets] --->他們插入到Eprescriber形式並保存爲record.at開始有這些字段。患者姓名&他的SelectedTablets

但是當我要開發它給我一個下面的error.i請參閱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; 
    } 

} 

這裏

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

這裏另一個bean類是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) 
+3

請添加堆棧跟蹤。已添加 –

+0

錯誤代碼 –

回答

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

SelectedTablets.java編輯如上所述。現在我的問題排序& tha nks 4全部