0

this is the parent entity this is the child entityi have tried the suggested solution to similar error above but my application still spits out the same exception. please i need help

below is the exception

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

public class 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);   

please help. thanks

1 Answers1

0

i have resolved the problem. And in addition i found out how i can actually save just the parent class by calling the save method on the parent and then it saves the child entities as well. jpa makes it quite easy. i will use an illustration of employee and address. lets say an employee can have more than one address it means we will have two tables in the database and two entity classes, one for each of these tables right?. it also means that the employee table becomes the parent table while the address becomes the child table right? the code below just shows how you can save the parent class and by the saving the parent class the child entities get saved as well! all you do is set the parent object(in this case the employee) on the child object(in this case the address), then set the List(the list of child in this case is address. so it becomes List) on the parent entity. then call the save method on the parent.

...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();


}

}

i hope that helps some people..thanks