OneToMany关系不保存外键

时间:2015-12-08 18:19:20

标签: java hibernate rest

我正在hibernate查询中建立OneToMany关系。它工作正常,但donorId没有进入blood表。

捐助者类别:

@Entity
@Table(name = "DONOR")
public class Donor {

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int donorId;
private String firstName;
private String lastName;
private int age;
private String cnic;
private String contactNumber;
private String homeNumber;
private String country;
private String city;
private String town;
private String streetNo;
private String houseNo;
private String email;

@OneToMany(mappedBy="donor")
private Set<Blood> blood;

public int getDonorId() {
    return donorId;
}
public void setDonorId(int donorId) {
    this.donorId = donorId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getCnic() {
    return cnic;
}
public void setCnic(String cnic) {
    this.cnic = cnic;
}
public String getContactNumber() {
    return contactNumber;
}
public void setContactNumber(String contactNumber) {
    this.contactNumber = contactNumber;
}
public String getHomeNumber() {
    return homeNumber;
}
public void setHomeNumber(String homeNumber) {
    this.homeNumber = homeNumber;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getTown() {
    return town;
}
public void setTown(String town) {
    this.town = town;
}
public String getStreetNo() {
    return streetNo;
}
public void setStreetNo(String streetNo) {
    this.streetNo = streetNo;
}
public String getHouseNo() {
    return houseNo;
}
public void setHouseNo(String houseNo) {
    this.houseNo = houseNo;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

public Set<Blood> getBlood() {
    return blood;
}

public void setCity(Set<Blood> blood) {
    this.blood = blood;
}
}

血型:

@Entity
@Table(name = "BLOOD")
public class Blood {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int bloodId;
private String bloodType;
private int price;
@ManyToOne
@JoinColumn(name="donor_id")
private Donor donor;

public int getBloodId() {
    return bloodId;
}
public void setBloodId(int bloodId) {
    this.bloodId = bloodId;
}
public String getBloodType() {
    return bloodType;
}
public void setBloodType(String bloodType) {
    this.bloodType = bloodType;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}

public Donor getDepartment() {
    return donor;
}

public void setDonor(Donor donor) {
    this.donor = donor;
}   
}

Blood And Donor Object类:

public class BloodDonor {

private Donor donor;
private Blood blood;
public Donor getDonor() {
    return donor;
}
public void setDonor(Donor donor) {
    this.donor = donor;
}
public Blood getBlood() {
    return blood;
}
public void setBlood(Blood blood) {
    this.blood = blood;
}
}

BloodService Class:

public class BloodService {

SessionFactory sessionFactory = null;

public BloodDonor addNewBlood(BloodDonor bloodDonor){
    try{
        Blood blood = new Blood();
        Donor donor = new Donor();
        blood = (Blood)bloodDonor.getBlood();
        donor = (Donor)bloodDonor.getDonor();
        sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(donor);
        session.save(blood);
        session.getTransaction().commit();
        session.close();

    }catch(Exception ex){
        ex.printStackTrace();
    }
    return bloodDonor;
}
}

血液资源:

@Path("blood")
public class BloodResource {

BloodService bloodService = new BloodService();

@Path("new")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public BloodDonor addNewDonor(BloodDonor blood){
    return bloodService.addNewBlood(blood);
}
}

控制台:

Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into DONOR (age, city, cnic, contactNumber, country, email, firstName, homeNumber, houseNo, lastName, streetNo, town, donorId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into BLOOD (bloodType, donor_id, price, bloodId) values (?, ?, ?, ?)

1 个答案:

答案 0 :(得分:0)

<强>首先

添加CascadeType:

@OneToMany(cascade = CascadeType.ALL, mappedBy="donor")
private Set<Blood> blood;

<强>第二

您的实体应该相互连接,例如捐赠者的Set<Blood>应该包含血液和血液的字段Donor的链接应设置为该捐赠者实例。

所以,让我们尝试互连它们:

Session session = sessionFactory.openSession();
session.beginTransaction();
Donor donor = new Donor();
Blood blood = new Blood();
blood.setDonor(donor);
HashSet<Blood> bloods = new HashSet<Blood>();
bloods.add(blood);
donor.setBlood(bloods);
//set some another fields if you want or they are NOT NULL in database
session.save(donor); //blood should be saved automatically due to cascade
session.getTransaction().commit();
session.close();

如果您的数据库结构一切正常,这应该可行。