/ / Hibernacja Jeden do wielu błędów mapowania - wiosna, hibernacja, jeden-do-wielu, dao

Hibernacja Jeden do wielu błędów mapowania - wiosna, hibernacja, jeden-do-wielu, dao

Zamierzam rozwinąć system zarządzania szpitalem. W tym miejscu jest mapowanie, jak poniżej.

lekarz może wybrać tabletki [wybrane Tabele] ---> wstawiono je do formularza Eprescribera i zapisano jako rekord. Na początek są te pola. pacjent Nazwa i Wybrane Tabele dla niego

Ale kiedy mam zamiar rozwinąć to daje mi poniżej error.i refer Mkyong Poradniki dla mojej części hibernacji.

pomóż mi rozwiązać ten problem. tutaj są moje zajęcia z fasoli. czy jest jakiś problem z moimi mapowaniami z moją logiką biznesową

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

}

inna klasa fasoli tutaj

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

tutaj jest metoda klasy DAO. wydaje się, że tutaj jest mój problem. ? ? ?

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

jeśli popełnisz błąd w stosunku do mojego DAO, to proszę o poradę również dla poprawienia tych błędów

-dzięki

dziennik błędów

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)

Odpowiedzi:

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

SelectedTablets.java edytowane jako powyżej.now mój problem posortowane i dzięki 4 wszystkie