插入表

时间:2017-02-07 13:55:21

标签: java mysql jpa eclipselink persistence

首先回顾一下故事。我遇到的问题是我创建用户的时间。以前我曾尝试创建一个用户并在发现之前单独为它们分配一个角色,通过插入SEC_USER_ROLE表,程序也插入到APP_USER表中,我收到一个关于将重复值插入父表的错误。但是,现在通过一起创建用户和角色,我收到以下错误:

  

主键应该是原始的(或复合的基元列表)   pk),java.lang.Long的一个实例,主键填写或   WebIntSecRole的一个实例.......

代码如下,不知道我在哪里出错或者是最好的解决方案。

Admin.java:

            //New User Creation
    WebIntUser newUser = new WebIntUser();
    newUser.setLoginId(newLoginName);
    newUser.setCreatedBy(loggedUser);
    newUser.setCreatedOn(today);
    newUser.setDbAuth(true);
    newUser.setDeleted(false);
    newUser.setDisabled(false);
    newUser.setEmail(newEmail);
    newUser.setEncrypted(true);
    newUser.setEncryptPassword(true);
    newUser.setFirstName(newFirstName);
    newUser.setLastName(newLastName);
    newUser.setUpdatedBy(loggedUser);
    newUser.setUpdatedOn(today);
    newUser.setVersion(1);
    newUser.setLdapId(1);
    //userService.createUser(newUser);

    //Set role for new user
    WebIntSecRoleUser newUserRole = new WebIntSecRoleUser();
    newUserRole.setUser(newUser);
    newUserRole.setDeleted(false);
    newUserRole.setRole(userService.selectRoleById(1));
    //newUserRole.setCreatedBy(loggedUser);
    //newUserRole.setCreatedOn(today);
    //newUserRole.setUpdatedBy(loggedUser);
    //newUserRole.setUpdatedOn(today);
    newUserRole.setVersionNumber(0);
    userService.createRole(newUserRole);

WebIntUser.java

    @Entity
@Table(name = "APP_USER")
@EntityListeners(value = { AuditChangeListener.class })
public class WebIntUser implements Serializable {
public WebIntUser() {
};

public WebIntUser(String login, String pass) {
    this.loginId = login;
    this.password = pass;
}

private Integer userId;
private String loginId;
private String password;
private String firstName;
private String lastName;
private String email;
private boolean disabled;
private boolean deleted;
private boolean dbAuth;
private boolean isEncrypted;
private boolean encryptPassword;
private Date lastLogin;
private Date prevLogin;
private Integer version;
private Date lastPasswordChange;
private Date createdOn;
private Date updatedOn;
private String createdBy;
private String updatedBy;
private Integer ldapId;

public static interface propertyName {
    String userId = "userId";
    String loginId = "loginId";
    String password = "password";
    String firstName = "firstName";
    String lastName = "lastName";
    String email = "email";
    String disabled = "disabled";
    String deleted = "deleted";
    String dbAuth = "dbAuth";
    String isEncrypted = "isEncrypted";
    String encryptPassword = "encryptPassword";
    String lastLogin = "lastLogin";
    String prevLogin = "prevLogin";
    String version = "version";
    String lastPasswordChange = "lastPasswordChange";
    String createdOn = "createdOn";
    String updatedOn = "updatedOn";
    String createdBy = "createdBy";
    String updatedBy = "updatedBy";
    String ldapId = "ldapId";
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID", nullable = false)
public Integer getUserId() {
    return userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}
.....getters/setters
}

WebIntSecRoleUser.java:

@Entity
@Table(name = "SEC_ROLE_USER")
@EntityListeners(value = {AuditInfoChangeListener.class})
public class WebIntSecRoleUser implements AuditableDomainObject {

private Long id;
private WebIntSecRole role;
private WebIntUser user;
private boolean deleted;
private AuditInfo auditInfo;
private long versionNumber;
private Date createdOn;
private Date updatedOn;
private String createdBy;
private String updatedBy;

public interface propertyName extends Auditable.propertyName {
    String id="id";
    String role="role";
    String user="user";
    String deleted = "deleted";
    String createdOn = "createdOn";
    String updatedOn = "updatedOn";
    String createdBy = "createdBy";
    String updatedBy = "updatedBy";
}

public static interface permissionKey{
   String UPDATE="SecRoleUser.U";
}

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ROLE_USER_ID",nullable = false, unique = true)
public Long getId() {
    return id;
}

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

@JoinColumn(name="ROLE_ID", nullable=false)
public WebIntSecRole getRole() {
    return role;
}

public void setRole(WebIntSecRole role) {
    this.role = role;
}

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name="USER_ID", nullable = false)
public WebIntUser getUser() {
    return user;
}

public void setUser(WebIntUser user) {
    this.user = user;
}
Getters/setters
}

注意:有一些注释掉的代码,我要么不再使用了,或者在Created By和Created On等的情况下,我收到多个插入的错误。

1 个答案:

答案 0 :(得分:0)

在我看来,你错过了WebIntSecRole上的@ManyToOne映射。您只指定了@JoinColumn。

@ManyToOne(/* desired options */)
@JoinColumn(name="ROLE_ID", nullable=false)
public WebIntSecRole getRole() {
    return role;