1

I am having three class. Person,vehicle and a association class to link the person and vehicle

Person

package entity;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name ="PERSON")
public class Person {
@EmbeddedId
private PKperson pkPerson;

@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade=CascadeType.ALL,mappedBy="person")
private Set<PersonVehAssnVO> personVehAssnVOSet=new HashSet<PersonVehAssnVO>();

//getters & setters
}

PKperson

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class PKperson implements Serializable {

@Column(name="NAME", nullable=false)
private String name;

@Column(name="DOB_DT", nullable=false)
private Date dobDt;
}

Vehicle

package entity;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name ="VEHICLE")
public class Vehicle {
@Id
@Column(name="VEHICLE_ID",unique=true, nullable=false)  
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_name")
@SequenceGenerator(allocationSize = 1, name = "seq_name", sequenceName = "SEQ_VEHICLE_ID")
private Long vehicleId;

@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade=CascadeType.ALL,mappedBy="vehicle")
private Set<PersonVehAssnVO> personVehAssnVOSet=new HashSet<PersonVehAssnVO>();

private String vehicleName;

//getters & setters
}

Person Vehicle Association

package entity;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name ="PERSON_VEHICLE_ASSOC")
public class PersonVehAssnVO {
@EmbeddedId
private PKperson pkPerson;

@ManyToOne
@JoinColumns({ @JoinColumn(name = "pkPerson.name",referencedColumnName ="NAME"),
@JoinColumn(name = "pkPerson.dobDt",referencedColumnName ="DOB_DT")})
private Person person;

@ManyToOne
@JoinColumn(name = "VEHICLE_ID", referencedColumnName = "VEHICLE_ID")
private Vehicle vehicle;

//getters & setters
}


//**Save Method**
PKperson pkPerson = new PKperson();
SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dtFormat.parse("1984-12-14");
pkPerson.setName("Magesh");
pkPerson.setDobDt(date1);

Person person = new Person();
person.setPKperson(pkPerson);

Vehicle vehicle = new Vehicle();
vehicle.setName("Honda350");

PersonVehAssnVO perVehAssnVO = new PersonVehAssnVO();
PersonVehAssnVO.setPKperson(pkPerson);
PersonVehAssnVO.setVehicle(vehicle);

Set<PersonVehAssnVO>  assocSet = new HashSet<PersonVehAssnVO>();
assocSet.add(PersonVehAssnVO);

person.setpersonVehAssnVOSet(assocSet);
vehicle.setpersonVehAssnVOSet(assocSet);

Session session = getHibernateTemplate().getSessionFactory().openSession();
transaction = session.beginTransaction();

transaction.begin();    
session.save(person);       
transaction.commit();

//=============================================================

While Executing the above save logic I am getting error ": [entity.PersonVehAssnVO#component[name,dobDt]{dobDt=magesh, dobDt=1984-12-14 00:00:00}]"

Show Sql Gives "select personVehAssnVO_.NAME, personVehAssnVO_.DOB_DT, personVehAssnVO_.VEHICLE_ID as vehicle_34 from PERSON_VEHICLE_ASSOC where personVehAssnVO_.NAME=? and personVehAssnVO_.DOB_DT=?"

I want to save Person,Vehicle and association in a single save means CASCADE ALL When I save Person.

Any help appreciated

Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
Magesh
  • 221
  • 2
  • 10

1 Answers1

0

Mark the date in PKPerson with the @Temporal annotation:

@Temporal(TemporalType.TIME)
@Column(name="DOB_DT", nullable=false, length = 8)
private Date dobDt;

According to the Java Doc

This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.

The Temporal annotation may be used in conjunction with the Basic annotation, the Id annotation, or the ElementCollection annotation (when the element collection value is of such a temporal type.

And according to this SO question:

In plain Java APIs, the temporal precision of time is not defined. When dealing with temporal data you might want to describe the expected precision in database. Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.

I hope this solves your problem. You can interchange the TemporalType with others that match your field type in the database. The column length also can be changed accordingly.

Edit

I think you also need to remove the mappedBy="person" and mappedBy="vehicle" from the annotation of the PersonVehAssnVO sets in the Person and Vehicle classes and replace them with full column properties so that these classes can be the owners of the relationship. Otherwise when you save a person, it will ignore updating any properties marked with the mappedby annotation because this means the Person is not the owner of that relationship.

Community
  • 1
  • 1
  • I have tried it. No Luck. Correct me if I am wrong the error says there is no person(magesh,1984-12-14) to insert in association. Is hibernate expecting person should be saved before mapping it with Association? – Magesh Jul 25 '14 at 18:41
  • I think you need to remove the mapped by from the annotation of the `PersonVehAssnVO` sets in the Person and Vehicle classes and replace it with full column properties so that these classes can be the owners of the relationship. Otherwise when you save a person, it will ignore updating any properties marked with the mappedby annotation because this means the Person is not the owner of that relationship – Japheth Ongeri - inkalimeva Jul 26 '14 at 13:34