我有三节课。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")
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;
}
车辆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
}
人车协会
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();
//=============================================================
在执行上述保存逻辑时,我得到错误":[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=?
"
我想保存Person
, Vehicle
和association
在一个保存意味着CASCADE ALL
当我保存Person
。
感谢您的帮助
用@Temporal
注释在PKPerson中标记日期:
@Temporal(TemporalType.TIME)
@Column(name="DOB_DT", nullable=false, length = 8)
private Date dobDt;
根据Java Doc
必须为持久字段或属性指定此注释类型为java.util.Date和java.util.Calendar。它可能只是为这些类型的字段或属性指定。
时态注释可以与基本注释一起使用注释、Id注释或ElementCollection注释(当元素集合值为这种时态类型时)
根据这个SO问题:
我希望这能解决你的问题。您可以与数据库中与您的字段类型匹配的其他在普通Java api中,时间的时间精度没有定义。在处理时态数据时,您可能希望描述数据库中期望的精度。时态数据可以有DATE、TIME或时间戳精度(即实际日期,只有时间,或两者)。使用@Temporal注释对其进行微调。
TemporalType
交换。列的长度也可以相应地改变。
编辑
我认为您还需要从Person和Vehicle类中的PersonVehAssnVO
集的注释中删除mappedBy="person"
和mappedBy="vehicle"
,并用完整的列属性替换它们,以便这些类可以成为关系的所有者。否则,当您保存一个person时,它将忽略更新任何带有mappedby
注释的属性,因为这意味着person不是该关系的所有者。