2015-11-03 53 views
0

this is the parent entitythis is the child entity我已經嘗試了上述類似錯誤的建議解決方案,但我的應用程序仍然吐出相同的異常。請我需要幫助如何解決'保存瞬態實例保存'錯誤在休眠一對多關係jpa

下面是例外

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing: com.domkat.springmvcjpa.model.Cpfaceleft1angle.cpftid -> com.domkat.springmvcjpa.model.Fromtocp 
at org.hibernate.engine.spi.CascadingAction$8.noCascade(CascadingAction.java:380) 
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:177) 
at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:162) 
at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:153) 
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:89) 
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) 
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234) 
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) 
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) 
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) 
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75) 
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:515) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
at com.sun.proxy.$Proxy626.save(Unknown Source) 


@Entity 
@Table(name = "cpobservedhorizontalangles") 
public class Cpobservedhorizontalangles implements Serializable { 
private static final long serialVersionUID = 1L; 
@Column(name = "degree") 
private Integer degree; 
@Column(name = "minute") 
private Integer minute; 
@Column(name = "second") 
private Integer second; 
@Column(name = "degminsec") 
private String degminsec; 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "ohaid") 
private Integer ohaid; 
@OneToMany(cascade={CascadeType.ALL},mappedBy = "ohaid") 
private List<Cpfaceleft2angle> cpfaceleft2angleList; 
@OneToMany(cascade={CascadeType.ALL},mappedBy = "ohaid") 
private List<Cpfaceright2angle> cpfaceright2angleList; 
@OneToMany(cascade={CascadeType.ALL},mappedBy = "ohaid") 
private List<Cpfaceleft1angle> cpfaceleft1angleList; 
@OneToMany(cascade={CascadeType.ALL},mappedBy = "ohaid") 
private List<Cpfaceright1angle> cpfaceright1angleList; 
...getters and setters... this is the child entity 

@Entity 
@Table(name = "cpfaceleft1angle") 
public class Cpfaceleft1angle implements Serializable { 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "id") 
private Integer id; 
@OneToMany(cascade={CascadeType.PERSIST},mappedBy = "cpfl1id") 
private List<Fromtocp> fromtocpList; 
@JoinColumn(name = "ohaid", referencedColumnName = "ohaid") 
@ManyToOne 
private Cpobservedhorizontalangles ohaid; 
@JoinColumn(name = "faceid", referencedColumnName = "faceid") 
@ManyToOne 
private Faceleft faceid; 
...getters and setters... this is the parent class but it contains other 
entities 

public class Fromtocp implements Serializable { 
private static final long serialVersionUID = 1L; 
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation 
@Column(name = "distance") 
private Double distance; 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "ftcpid") 
private Integer ftcpid; 
@Column(name = "diffleft") 
private String diffleft; 
@Column(name = "diffright") 
private String diffright; 
@Column(name = "meandiff") 
private String meandiff; 
@Column(name = "oadegdec") 
private Double oadegdec; 
@Column(name = "fb") 
private Double fb; 
@Column(name = "bb") 
private Double bb; 
@JoinColumn(name = "fromcp", referencedColumnName = "cpid") 
@ManyToOne 
private Controlpoints fromcp; 
@JoinColumn(name = "cpfl1id", referencedColumnName = "id") 
@ManyToOne 
private Cpfaceleft1angle cpfl1id; 
@JoinColumn(name = "cpfl2id", referencedColumnName = "id") 
@ManyToOne 
private Cpfaceleft2angle cpfl2id; 
@JoinColumn(name = "cpfr1id", referencedColumnName = "id") 
@ManyToOne 
private Cpfaceright1angle cpfr1id; 
@JoinColumn(name = "cpfr2id", referencedColumnName = "id") 
@ManyToOne 
private Cpfaceright2angle cpfr2id; 
@JoinColumn(name = "tocp", referencedColumnName = "cpid") 
@ManyToOne 
private Controlpoints tocp; 
...this is the other entity the parent class contains. 

@Controller 

公共類SurveyController {

@Autowired 
private SurveyService ss; 
@Autowired 
private ControlPointService cps; 
@Autowired 
private ScpService sps; 
@Autowired 
private CpobservedhorizontalanglesService cpos; 
@Autowired 
private Cpfaceleft1angleService cpfl1; 
@Autowired 
private Cpfaceleft2angleService cpfl2; 
@Autowired 
private Cpfaceright1angleService cpfr1; 
@Autowired 
private Cpfaceright2angleService cpfr2; 
@Autowired 
private FromTocpService ftcps; 
@Autowired 
private FaceleftService fls; 
@Autowired 
private FacerightService frs; 

private Processor processor = new Processor(); 
HttpSession session; 

@RequestMapping(value = "/surveydetails") 
public String showSurveyDetailsPage(Model model) { 
    Surveys survey = new Surveys(); 
    model.addAttribute("survey", survey); 
    return "SurveyDetails"; 
} 

@RequestMapping(value = "/stations", method = RequestMethod.POST) 
public String createSurvey(@RequestParam("surveyTitle") String title, 
     @RequestParam("cp1Label") String cp1Label, @RequestParam("cp1Northings") double northingsCp1, 
     @RequestParam("cp1Eastings") double eastingsCp1, @RequestParam("cp2Label") String cp2Label, 
     @RequestParam("cp2Northings") double northingsCp2, @RequestParam("cp2Eastings") double eastingsCp2, 
     @RequestParam("distance") double distance, @RequestParam("fl1Deg") int fl1Deg, 
     @RequestParam("fl1Min") int fl1Min, @RequestParam("fl1Sec") int fl1Sec, 
     @RequestParam("fl2Deg") int fl2Deg, @RequestParam("fl2Min") int fl2Min, @RequestParam("fl2Sec") int fl2Sec, 
     @RequestParam("fr1Deg") int fr1Deg, @RequestParam("fr1Min") int fr1Min, @RequestParam("fr1Sec") int fr1Sec, 
     @RequestParam("fr2Deg") int fr2Deg, @RequestParam("fr2Min") int fr2Min, @RequestParam("fr2Sec") int fr2Sec) { 
    Surveys survey = new Surveys(); 
    Scp scp = new Scp(); 
    Signups su = null; 

    Cpobservedhorizontalangles o1 = new Cpobservedhorizontalangles(); 
    o1.setDegree(fl1Deg); 
    o1.setMinute(fl1Min); 
    o1.setSecond(fl1Sec); 
    o1.setDegminsec(processor.degToString(fl1Deg, fl1Min, fl1Sec)); 
Cpfaceleft1angle fl1 = new Cpfaceleft1angle(); 
    fl1.setOhaid(o1); 
    fl1.setFaceid(faceleft1); 
    cpos.save(o1); 
    cpfl1.save(fl1); 

請幫助。謝謝

+0

您需要級聯如果任何關係/協會出現在你的類。由於該關聯需要由父類保持。 –

+1

@Domkat B Stephen請發佈您的實體 –

+1

add'@OneToOne(cascade = {CascadeType.ALL})'see [this](http://stackoverflow.com/questions/2302802/object-references-an-unsaved-transient -instance-save-the-transient-instance-be)鏈接 –

回答

0

我已經解決了這個問題。此外,我發現如何通過調用父級的保存方法實際上保存父類,然後保存子實體。 jpa使它非常容易。 我將使用員工和地址的插圖。可以說員工可以有多個地址,這意味着我們將在數據庫中有兩個表和兩個實體類,每個表都對應一個表。這也意味着員工表成爲父表而地址成爲子表正確嗎?下面的代碼只是展示瞭如何保存父類,並通過保存父類來保存子實體!你所要做的就是在子對象(本例中爲地址)上設置父對象(在本例中爲員工),然後在父對象上設置List(在這種情況下子對象的列表是地址,因此它變成List)實體。然後調用父級上的save方法。

...necessary imports... 
@Entity 
@Table(name = "address") 
@NamedQueries({ 
@NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a")}) 
public class Address implements Serializable { 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "address_id") 
private Integer addressId; 
@Column(name = "employee_address") 
private String employeeAddress; 
@JoinColumn(name = "employee_id", referencedColumnName = "employee_id") 
@ManyToOne 
private Employee employeeId; 

public Address() { 
} 
...getters and setters for this entity... 

@Entity 
@Table(name = "employee") 
@NamedQueries({ 
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")}) 
public class Employee implements Serializable { 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Basic(optional = false) 
@Column(name = "employee_id") 
private Integer employeeId; 
@Column(name = "name") 
private String name; 
@Column(name = "email") 
private String email; 
@Column(name = "phone") 
private String phone; 
@OneToMany(cascade=CascadeType.ALL,mappedBy = "employeeId") 
private List<Address> addressList; 


public Employee() { 
} 
...getters and setters for this class... 

...below is the class that makes use of the entities... 

public class SaveExample { 

public static void main(String[] args) { 
    EntityManagerFactory emf =  Persistence.createEntityManagerFactory("JPAJoinTableTutorialsPU"); 
    EntityManager em = emf.createEntityManager(); 
    em.getTransaction().begin(); 

    Employee employee = new Employee();// employee object 
    Address address = new Address(); // Address Object 
    address.setEmployeeAddress("set the value"); 
    address.setEmployeeId(employee); 
    List<Address>addressList=new ArrayList<>(); 
    addressList.add(address); 


    employee.setEmail("set the value"); 
    employee.setName("set the value for name"); 
    employee.setPhone("set the value"); 
    employee.setAddressList(addressList); 

    em.persist(employee); 

    em.getTransaction().commit(); 


} 

}

我希望幫助一些people..thanks

相關問題