简体   繁体   中英

JPA Eclipselink error inserting into table

So first a bit of back story. The issue I am having is when I create a user. Previously I had tried to create a user and assign them a role separately before discovering that by inserting into the SEC_USER_ROLE table the program was also inserting into the APP_USER table and I was getting an error about inserting duplicate values into the parent table. However, now by creating the user and role together I am getting the following error:

Primary key should be primitive (or list of primitives for composite pk) , an instance of java.lang.Long with the primary keys filled in or an instance of WebIntSecRole.......

Code as follows, not sure where I'm goin g wrong or the best solution at this point.

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
}

Note: There is some commented out code that I'm either trying not to use anymore, or in the case of Created By and Created On etc I was getting errors for multiple inserts.

In my opinion you have missed the @ManyToOne mapping on the WebIntSecRole . You only specified the @JoinColumn.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM