【问题标题】:Issue in JPA Cascade Save with EmbeddedId in childJPA Cascade Save 中的问题与子项中的 EmbeddedId
【发布时间】:2014-07-25 15:10:04
【问题描述】:

我正在上三节课。 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}]"

显示 Sql 给出“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 时。

任何帮助表示赞赏

【问题讨论】:

    标签: java hibernate jpa embeddable


    【解决方案1】:

    用@Temporal注解在PKPerson中标记日期:

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

    根据the Java Doc

    必须为持久字段或属性指定此注解 java.util.Date 和 java.util.Calendar 类型的。它可能只是 为这些类型的字段或属性指定。

    Temporal 注释可以与 Basic 结合使用 注释、Id 注释或 ElementCollection 注释 (当元素集合值是这种时间类型时。

    并根据this SO question:

    在普通的 Java API 中,没有定义时间的时间精度。 在处理时态数据时,您可能想要描述 数据库中的预期精度。时态数据可以有 DATE、TIME 或 TIMESTAMP 精度(即实际日期、仅时间或两者兼有)。利用 @Temporal 注释来微调它。

    我希望这能解决您的问题。您可以将TemporalType 与数据库中与您的字段类型匹配的其他人互换。列长也可以相应改变。

    编辑

    我认为您还需要从 Person 和 Vehicle 类中 PersonVehAssnVO 集的注释中删除 mappedBy="person" 和 mappedBy="vehicle" 并将它们替换为完整的列属性,以便这些类可以成为关系。否则,当您保存人员时,它将忽略更新任何标有 mappedby 注释的属性,因为这意味着该人员不是该关系的所有者。

    【讨论】:

    • 我试过了。没运气。如果我错了,请纠正我,错误说没有人(magesh,1984-12-14)可以插入关联。在将其与关联映射之前是否应保存休眠期望的人?
    • 我认为您需要从 Person 和 Vehicle 类中 PersonVehAssnVO 集的注释中删除映射的依据,并将其替换为完整的列属性,以便这些类可以成为关系的所有者.否则,当您保存一个人时,它将忽略更新任何标有 mappedby 注释的属性,因为这意味着该人不是该关系的所有者
    猜你喜欢
    • 2012-03-31
    • 2023-04-03
    • 2016-09-08
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多