spring data jpa抛出异常并且不会保留数据

时间:2014-07-07 07:18:01

标签: java spring spring-mvc jpa eclipselink

我有一个Java Spring 4应用程序,我试图坚持一个实体。但相反,我得到了一个例外:

Invalid property 'id' of bean class [my.backend.domain.SystemUser]: Getter for property 'id' threw exception. 

数据未存储在数据库中。一般的数据库设置工作,我已经用它登录。

我有服务类调用Spring Data JPA方法:

public SystemUser create(SystemUser systemUser) {
    SystemUser newUser = systemUserRepository.saveAndFlush(systemUser);
    return newUser;
}

调试器显示systemUser实例包含以下值:

systemUser = {my.backend.domain.SystemUser@20651}
address = {java.lang.String@20689}"asdf"
companyName = {java.lang.String@20688}"asdf"
costCenter = {java.lang.String@20697}"asdf"
customerNumber = {java.lang.String@20696}"asdf"
email = {java.lang.String@20695}"asdf@asdf.com“
fax = {java.lang.String@20694}"asdfadsf"
id = null
location = {java.lang.String@20691}"df"
password = null
phone = {java.lang.String@20693}"asdf"
primaryContact = {java.lang.String@20692}"fdfd"
roles = {java.util.ArrayList@20687} size = 2
userType = {my.backend.domain.UserType@20686}"CLIENT"
zipCode = {java.lang.String@20690}"asdf"

这是我的SystemUser实体(我使用EclipseLink):

package my.backend.domain;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "system_users")
public class SystemUser implements Serializable {

public static final long serialVersionUID = 42L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "password")
private String password;

@Column(name = "usertype")
@Enumerated(EnumType.STRING)
private UserType userType;

@ManyToMany
@JoinTable(name = "system_user_role",
        joinColumns = {
                @JoinColumn(
                        name = "system_user_id",
                        referencedColumnName = "id"
                )
        },
        inverseJoinColumns =
                @JoinColumn(
                        name = "role_id",
                        referencedColumnName = "id"
                )

)
private List<Role> roles = new ArrayList<Role>();

@Column(name = "companyname")
private String companyName;

@Column(name = "address")
private String address;

@Column(name = "zipcode")
private String zipCode;

@Column(name = "location")
private String location;

@Column(name = "primarycontact")
private String primaryContact;

@Column(name = "phone")
private String phone;

@Column(name = "fax")
private String fax;

@Column(name = "email")
private String email;

@Column(name = "customernumber")
private String customerNumber;

@Column(name = "costcenter")
private String costCenter;

public SystemUser() {
}

public SystemUser(String username) {
}

/* getters and setters */

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public UserType getUserType() {
    return userType;
}

public void setUserType(UserType userType) {
    this.userType = userType;
}

public List<Role> getRoles() {
    return roles;
}

public void setRoles(List<Role> roles) {
    this.roles = roles;
}

public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getZipCode() {
    return zipCode;
}

public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getPrimaryContact() {
    return primaryContact;
}

public void setPrimaryContact(String primaryContact) {
    this.primaryContact = primaryContact;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getFax() {
    return fax;
}

public void setFax(String fax) {
    this.fax = fax;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCustomerNumber() {
    return customerNumber;
}

public void setCustomerNumber(String customerNumber) {
    this.customerNumber = customerNumber;
}

public String getCostCenter() {
    return costCenter;
}

public void setCostCenter(String costCenter) {
    this.costCenter = costCenter;
}

}

摘要:数据不会保留,saveAndFlush(systemUser)不会生成ID,因此会引发上述异常。

如果我错过了提供任何信息,请告诉我,我会立即添加。

============================ 感谢vbera,我发现了一个非常令人尴尬的问题:id的getter和setter长而不是Long。

现在我有一个例外:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '0' 
for key 'PRIMARY'
Error Code: 1062
Call: INSERT INTO system_users (address, companyname, costcenter, customernumber, email, fax, 
location, password, phone, primarycontact, usertype, zipcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 
?, ?, ?, ?)
bind => [asdf, asdf, asdf, asdf, asdf@asdf.com, fsad, asdf, null, asfd, asf, CLIENT, asdf]
Query: InsertObjectQuery(my.backend.domain.SystemUser@2d787ac2)

2 个答案:

答案 0 :(得分:0)

你的id的getter-setter方法应该是:

public Long getId() { // Long instead of long
    return id;
} 

public void setId(Long id) { // Long instead of long
    this.id = id;
} 

答案 1 :(得分:-1)

是否缺少Id的注释? 喜欢:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;