列xxx中的空值违反非空约束

时间:2016-12-06 14:39:42

标签: java hibernate

我有员工

foreach(var item in result.events)
{
    Console.WriteLine(item.venue.name);
}

地址

@Entity
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer employeeId;

@Basic(optional=false)
private String employeeName;

@OneToMany(cascade=CascadeType.ALL) 
@JoinColumns({
@JoinColumn(name="employeeId",referencedColumnName="employeeId",
    updatable = false, insertable = true)
})
private List<Address> address;
//getters and setters
}

AddressPK

@Entity(name="address")
@Table(uniqueConstraints = { @UniqueConstraint(columnNames =     {"employeeId", "AddressId"})})
public class Address {

private String addressCountry;
private String addressCity;

@AttributeOverrides({ 
    @AttributeOverride(name = "employeeId", column = @Column(name = "employeeId", nullable = false))//, 
    //@AttributeOverride(name = "idCorporativeEvent", column = @Column(name = "id_corporative_event", nullable = false))
})
@EmbeddedId
private AddressPK addressId;
//getters and setters
}

我创建了对象Employee并尝试保存它:     员工员工=新员工();     //employee.setEmployeeId(1);     employee.setEmployeeName(&#34; John Smith&#34;);

@Embeddable
public class AddressPK implements Serializable{
@Column(name="employeeId", insertable = true, updatable = true)
private Integer employeeId;

@Column(name="AddressId")
private Integer addressId;

public Integer getEmployeeId() {
    return employeeId;
}
public void setEmployeeId(Integer employeeId) {
    this.employeeId = employeeId;
}

public Integer getAddressId() {
    return addressId;
}
public void setAddressId(Integer addressId) {
    this.addressId = addressId;
}

public boolean equals(Object obj){
    if(obj instanceof AddressPK){
        AddressPK addPK = (AddressPK) obj;
        if(this.addressId == addPK.getAddressId() &&
                this.employeeId == addPK.getEmployeeId()
                ){
            return true;
        }
    }
    else {
        return false;
    }
    return false;
}

public int hashCode(){
    return super.hashCode();
}
}

我收到错误:

Address address = new Address();

AddressPK addressPK = new AddressPK();
addressPK.setAddressId(1);
//addressPK.setEmployeeId(1);

address.setAddressId(addressPK);
address.setAddressCountry("United Kingdom");
address.setAddressCity("London");
List<Address> listAddr = new ArrayList<Address>();
listAddr.add(address);

employee.setAddress(listAddr);

serviceUser.save(empl);

当我删除

ERROR: null value in column "employeeid" violates not-null constraint: Failing row contains (1, null, London, United Kingdom).

并添加

@GeneratedValue(strategy = GenerationType.SEQUENCE)

我成功了。为什么我得到错误空值? 感谢&#39; S

0 个答案:

没有答案