JPA关系不能一对一地工作

时间:2016-06-07 15:58:51

标签: java hibernate jpa

我的第一张桌子

CREATE TABLE `DEVICE_REGISTRATION` (
      `DEV_ID` bigint(20) NOT NULL AUTO_INCREMENT,
      `PARTY_PTY_ID` int(11) NOT NULL,
      `PTY_AUTH_PIN` varchar(45) DEFAULT NULL,
      `COOKIE` varchar(45) DEFAULT NULL,
      `DEVICE_ID` varchar(45) DEFAULT NULL,
      `STATUS` varchar(45) DEFAULT NULL,
      `PNS_DEVICE_ID` varchar(45) DEFAULT NULL,
      PRIMARY KEY (`DEV_ID`),
      KEY `fk_PARTY_AUTH_MECH_PARTY2_idx` (`PARTY_PTY_ID`),
      CONSTRAINT `fk_PARTY_AUTH_MECH_PARTY2` FOREIGN KEY (`PARTY_PTY_ID`) REFERENCES `PARTY` (`PTY_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

这是我的聚会表

CREATE TABLE `PARTY` (
  `PTY_ID` int(11) NOT NULL AUTO_INCREMENT,
  `PTY_NAME` varchar(45) DEFAULT NULL,
  `PTY_SECOND_NAME` varchar(45) DEFAULT NULL,
  `PTY_ADDRESS_LINE_1` varchar(45) DEFAULT NULL,
  `PTY_ADDRESS_LINE_2` varchar(45) DEFAULT NULL,
  `PTY_ADDRESS_LINE_3` varchar(45) DEFAULT NULL,
  `PTY_TOWN` varchar(45) DEFAULT NULL,
  `PTY_POST_CODE` varchar(45) DEFAULT NULL,
  `PTY_COUNTY` varchar(45) DEFAULT NULL,
  `PTY_MOBILE` varchar(45) DEFAULT NULL,
  `PTY_EMAIL` varchar(45) DEFAULT NULL,
  `PTY_TITLE` varchar(45) DEFAULT NULL,
  `PTY_DOB` varchar(45) DEFAULT NULL,
  `PTY_HOUSE_NUMBER` varchar(45) DEFAULT NULL,
  `PIN` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`PTY_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=184 DEFAULT CHARSET=latin1;

这是我的恩典课

  @Entity

@Table(name="PARTY")
public class Party implements Serializable {


    /**
     * 
     */
    private static final long serialVersionUID = -193597591528824837L;



    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PTY_ID")    
    private long partyId ;





@JsonProperty ("deviceRegistration")
@OneToMany(cascade=CascadeType.ALL,mappedBy = "party")
private List<DeviceRegistration> deviceRegistration;


@JsonProperty ("wishList")
@OneToMany(cascade=CascadeType.ALL,orphanRemoval=false,fetch=FetchType.EAGER)
@JoinColumn(name="PTY_ID")
private List<WishList> wishList;

@JsonProperty ("first_name")
@Column(name = "PTY_NAME")
private String partyName ;

@JsonProperty ("address_1")
@Column(name = "PTY_ADDRESS_LINE_1")
private String partyAddressLine1 ;

@JsonProperty ("address_2")
@Column(name = "PTY_ADDRESS_LINE_2")
private String partyAddressLine2 ;

@JsonProperty ("address_3")
@Column(name = "PTY_ADDRESS_LINE_3")
private String partyAddressLine3 ;

@JsonProperty ("town")
@Column(name = "PTY_TOWN")
private String partyTown ;

@JsonProperty ("post_code")
@Column(name = "PTY_POST_CODE")
private String partyPostCode ;

/*@JsonProperty ("country")
@Column(name = "PTY_COUNTRY")
private String partyCountry 
change by Manoj Singh
*/

@JsonProperty ("county")
@Column(name = "PTY_COUNTY")
private String partyCounty ;



@JsonProperty ("phone_number")
@Column(name = "PTY_MOBILE")
private String partyMobile ;

@JsonProperty ("email")
@Column(name = "PTY_EMAIL")
private String partyEmail ; 

@JsonProperty ("second_name")
@Column(name = "PTY_SECOND_NAME")
private String secondName ;

@JsonProperty ("date_of_birth")
@Column(name = "PTY_DOB")
private String dateOfBirth ;

@JsonProperty ("title")
@Column(name = "PTY_TITLE")
private String title ;

@JsonProperty ("house_number")
@Column(name = "PTY_HOUSE_NUMBER")
private String houseNumber ;


@JsonProperty ("pass_code")
@Column(name = "PIN")
private String PIN ;

@Transient
@JsonProperty ("confirm_pin")   
private String oldPIN ;


public String getHouseNumber() {
    return houseNumber;
}

public void setHouseNumber(String houseNumber) {
    this.houseNumber = houseNumber;
}

public String getPIN() {
    return PIN;
}

public void setPIN(String pIN) {
    PIN = pIN;
}

public String getOldPIN() {
    return oldPIN;
}

public void setOldPIN(String oldPIN) {
    this.oldPIN = oldPIN;
}

public long getPartyId() {
    return partyId;
}

public void setPartyId(long partyId) {
    this.partyId = partyId;
}

public String getPartyName() {
    return partyName;
}

public void setPartyName(String partyName) {
    this.partyName = partyName;
}

public String getPartyAddressLine1() {
    return partyAddressLine1;
}

public void setPartyAddressLine1(String partyAddressLine1) {
    this.partyAddressLine1 = partyAddressLine1;
}

public String getPartyAddressLine2() {
    return partyAddressLine2;
}

public void setPartyAddressLine2(String partyAddressLine2) {
    this.partyAddressLine2 = partyAddressLine2;
}

public String getPartyAddressLine3() {
    return partyAddressLine3;
}

public void setPartyAddressLine3(String partyAddressLine3) {
    this.partyAddressLine3 = partyAddressLine3;
}

public String getPartyTown() {
    return partyTown;
}

public void setPartyTown(String partyTown) {
    this.partyTown = partyTown;
}

public String getPartyPostCode() {
    return partyPostCode;
}

public void setPartyPostCode(String partyPostCode) {
    this.partyPostCode = partyPostCode;
}



public String getPartyMobile() {
    return partyMobile;
}

public void setPartyMobile(String partyMobile) {
    this.partyMobile = partyMobile;
}

public String getPartyEmail() {
    return partyEmail;
}

public void setPartyEmail(String partyEmail) {
    this.partyEmail = partyEmail;
}

public String getSecondName() {
    return secondName;
}

public void setSecondName(String secondName) {
    this.secondName = secondName;
}

public String getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getPartyCounty() {
    return partyCounty;
}

public void setPartyCounty(String partyCounty) {
    this.partyCounty = partyCounty;
}
public List<DeviceRegistration> getDeviceRegistration() {
    return deviceRegistration;
}

public void setDeviceRegistration(List<DeviceRegistration> deviceRegistration) {
    this.deviceRegistration = deviceRegistration;
}

}

这是我的第二堂课

 package com.savio.rest.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;


/**
 * 
 * @author Manoj Singh
 *
 *This class is Entity bean class 
 *it is Use to persist The device in db
 */
@Entity
@Table(name="DEVICE_REGISTRATION")
public class DeviceRegistration implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 7466031115272826215L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "DEV_ID")
    @JsonIgnoreProperties
    @JsonProperty ("DEV_ID")
    private long devId;

/*  @JsonIgnoreProperties
    @Column(name = "PARTY_PTY_ID")
    @JsonProperty ("PARTY_PTY_ID")
    private long  ID;*/

    @ManyToOne
    private Party party;


    @JsonIgnoreProperties
    @Column(name = "PTY_AUTH_PIN")  
    @JsonProperty ("PTY_AUTH_PIN")
    private String partyAuthPIN ;

    @JsonIgnoreProperties
    @Column(name = "COOKIE")
    @JsonProperty ("COOKIE")
    private String cookie ;

    @Column(name = "DEVICE_ID")
    @JsonProperty ("DEVICE_ID")
    private String deviceId ;

    @JsonIgnoreProperties
    @Column(name = "STATUS")
    @JsonProperty ("STATUS")
    private String status ;

    @JsonIgnoreProperties
    @Column(name = "PNS_DEVICE_ID")
    @JsonProperty ("PNS_DEVICE_ID")
    private String PNSDeviceId ;




    public long getDevId() {
        return devId;
    }
    public void setDevId(long devId) {
        this.devId = devId;
    }


    /**
     * @return partyAuthPIN
     ** this is partyAuthPIN
     */
    public String getPartyAuthPIN() {
        return partyAuthPIN;
    }

    /**
     **@param partyAuthPIN
     * this is partyAuthPIN
     */
    public void setPartyAuthPIN(String partyAuthPIN) {
        this.partyAuthPIN = partyAuthPIN;
    }

    public Party getParty() {
        return party;
    }
    public void setParty(Party party) {
        this.party = party;
    }
    /**
     * @return cookie
     * * this is cookie
     */
    public String getCookie() {
        return cookie;
    }
    /**
     * @param cookie
     ** this is cookie
     */
    public void setCookie(String cookie) {
        this.cookie = cookie;
    }
    /**
     * @return String
     ** this is String
     */
    public String getDeviceId() {
        return deviceId;
    }
    /**
     * @param deviceId
     * this is deviceId
     * 
     */
    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
    /**
     * @return status
     ** this is status
     */
    public String getStatus() {
        return status;
    }
    /**
     ** @param status
     * this is status
     */
    public void setStatus(String status) {
        this.status = status;
    }
    /**
     * @return PNSDeviceId
     * * this is PNSDeviceId
     */
    public String getPNSDeviceId() {
        return PNSDeviceId;
    }
    /**
     * @param pNSDeviceId
     ** this is pNSDeviceId
     */
    public void setPNSDeviceId(String pNSDeviceId) {
        PNSDeviceId = pNSDeviceId;
    }
}

我正试图坚持党派,但它给了我错误 列'PARTY_PTY_ID'不能为空

我正在使用JPA和hibernate

partyRepository.save(方);

我这样做

 DeviceRegistration deviceRegistration = new DeviceRegistration(); 
 deviceRegistration.setStatus("ENABLE"); 
 deviceRegistration.setDeviceId(party.getDeviceRegistration().get(0).getDevic‌​eId()); 
 deviceRegistration.setPNSDeviceId(""); 
 Party userDetails = (Party) partyRepository.save(party);

1 个答案:

答案 0 :(得分:0)

根据错误PARTY_PTY_ID为null。此列在 DeviceRegistration类,因为您使用deviceRegistration注释了CascadeType.ALL属性。当您尝试将deviceRegistration保存到数据库时,Party表也会更新。

@OneToMany(cascade=CascadeType.ALL,mappedBy = "party")
private List<DeviceRegistration> deviceRegistration;

所以你必须采用这种方法来妥善保存。

Party partyOb;
DeviceRegistration deviceregistrationob;

deviceregistrationob.setParty(partyOb); 
partyOb.setDeviceRegistration(deviceregistrationob);

您必须将party对象添加到DeviceRegistration对象。 然后将DeviceRegistration对象添加到partyObject

编辑: -

 DeviceRegistration deviceRegistration = new DeviceRegistration();       
 deviceRegistration.setStatus("ENABLE"); 
 deviceRegistration.setPNSDeviceId(""); 

//this part was missing in your code
List<DeviceRegistration> deviceRegistrationList = new ArrayList(); //creating a list of DeviceRegistration
deviceRegistration.setParty(party); //adding a party to the device 
deviceRegistrationList.add(deviceRegistration); //adding device to the list
party.setDeviceRegistration(deviceRegistrationList); //adding device list to the party.
//**end
deviceRegistration.setDeviceId(party.getDeviceRegistration().get(0).getDevic‌eId());//this line should be called after adding device list to the party. otherwise you'll get null pointer exception.
Party userDetails = (Party) partyRepository.save(party);