gpt4 book ai didi

java - org.apache.openjpa.persistence.PersistenceException : null keys not allowed (OpenJPA)

转载 作者:行者123 更新时间:2023-11-29 13:29:35 26 4
gpt4 key购买 nike

  • 我创建了一个包含一张表的数据库:users

    DROP DATABASE IF EXISTS DB_TEST;
    CREATE database DB_TEST;
    USE DB_TEST;

    -- --------------------------------------------------------
    --
    -- Table structure for table `User`
    --

    DROP TABLE IF EXISTS `Users`;
    CREATE TABLE IF NOT EXISTS `Users` (
    `UserID` bigint(20) AUTO_INCREMENT NOT NULL,
    `UserName` varchar(30) NOT NULL,
    `UserPassword` varchar(30) NOT NULL,
    `ImageID` bigint(20) default NULL,
    `SignUpDate` date default NULL,
    `SignOutDate` date default NULL,
    `UserDescription` varchar(30) default NULL,
    `CommunityVotes` bigint(20) default NULL,
    `ImpactScore` float default NULL,
    `LinkedInURL` varchar(2083) default NULL,
    `ModifyDate` timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
    PRIMARY KEY (`UserID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;`
  • 然后我尝试使用 Openjpa 添加用户:

    private static int addUser(String pUserName, String pUserPassword, String pUserDesc) 
    throws Exception
    {
    // this is the JPA entity for the table Users
    RelationalJPAUser newUser = new RelationalJPAUser();
    newUser.setUserName(pUserName);
    newUser.setUserPassword(pUserPassword);
    newUser.setUserDescription(pUserDesc);

    usersCount++;
    newUser.setUserID(new Integer(usersCount));

    mUserTransaction.begin();
    mEm.persist(newUser);
    mEm.flush();
    mUserTransaction.commit();
    }
  • 我收到异常:org.apache.openjpa.persistence.PersistenceException:不允许从flush()方法抛出空键。

这是实体类:

    package com.ligilo.server.storage.relational.mysql;

import ....
@Entity
@Table(name="users")
@NamedQuery(name="RelationalJPAUser.findAll", query="SELECT r FROM RelationalJPAUser r")
public class RelationalJPAUser implements Serializable, com.ligilo.server.storage.IUser
{
private static final long serialVersionUID = 1L;

@Id
private Integer userID;

private Integer communityVotes;

private BigInteger imageID;

private float impactScore;

private URL linkedInURL;

private Timestamp modifyDate;

@Temporal(TemporalType.DATE)
private Date signOutDate;

@Temporal(TemporalType.DATE)
private Date signUpDate;

private String userDescription;

private String userName;

private String userPassword;

//bi-directional many-to-one association to RelationalJPAAttachment
@OneToMany(mappedBy="user")
private List<RelationalJPAAttachment> attachments;

//bi-directional many-to-one association to RelationalJPAComment
@OneToMany(mappedBy="user")
private List<RelationalJPAComment> comments;

//bi-directional many-to-one association to RelationalJPAFollow
@OneToMany(mappedBy="user1")
private List<RelationalJPAFollow> follows1;

//bi-directional many-to-one association to RelationalJPAFollow
@OneToMany(mappedBy="user2")
private List<RelationalJPAFollow> follows2;

//bi-directional many-to-one association to RelationalJPALog
@OneToMany(mappedBy="user")
private List<RelationalJPALog> logs;

//bi-directional many-to-one association to RelationalJPAPost
@OneToMany(mappedBy="user")
private List<RelationalJPAPost> posts;

public RelationalJPAUser() {
}

public Integer getUserID() {
System.out.println("getting user ID " + userID.intValue());
return this.userID;
}

public void setUserID(Integer userID) {
this.userID = userID;
System.out.println("setting user id to " + userID.intValue());
}

public Integer getCommunityVotes() {
return this.communityVotes;
}

public void setCommunityVotes(Integer communityVotes) {
this.communityVotes = communityVotes;
}

public BigInteger getImageID() {
return this.imageID;
}

public void setImageID(BigInteger imageID) {
this.imageID = imageID;
}

public Float getImpactScore() {
return this.impactScore;
}

public void setImpactScore(float impactScore) {
this.impactScore = impactScore;
}

public URL getLinkedInURL() {
return this.linkedInURL;
}

public void setLinkedInURL(URL linkedInURL) {
this.linkedInURL = linkedInURL;
}

public Timestamp getModifyDate() {
return this.modifyDate;
}

public void setModifyDate(Timestamp modifyDate) {
this.modifyDate = modifyDate;
}

public Date getSignOutDate() {
return this.signOutDate;
}

public void setSignOutDate(Date signOutDate) {
this.signOutDate = signOutDate;
}

public Date getSignUpDate() {
return this.signUpDate;
}

public void setSignUpDate(Date signUpDate) {
this.signUpDate = signUpDate;
}

public String getUserDescription() {
return this.userDescription;
}

public void setUserDescription(String userDescription) {
this.userDescription = userDescription;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
System.out.println("setting userName to " + userName);
}

public String getUserPassword() {
return this.userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public List<RelationalJPAAttachment> getAttachments() {
return this.attachments;
}

public void setAttachments(List<RelationalJPAAttachment> attachments) {
this.attachments = attachments;
}

public RelationalJPAAttachment addAttachment(RelationalJPAAttachment attachment) {
getAttachments().add(attachment);
attachment.setUser(this);

return attachment;
}

public RelationalJPAAttachment removeAttachment(RelationalJPAAttachment attachment) {
getAttachments().remove(attachment);
attachment.setUser(null);

return attachment;
}

public List<RelationalJPAComment> getComments() {
return this.comments;
}

public void setComments(List<RelationalJPAComment> comments) {
this.comments = comments;
}

public RelationalJPAComment addComment(RelationalJPAComment comment) {
getComments().add(comment);
comment.setUser(this);

return comment;
}

public RelationalJPAComment removeComment(RelationalJPAComment comment) {
getComments().remove(comment);
comment.setUser(null);

return comment;
}

public List<RelationalJPAFollow> getFollows1() {
return this.follows1;
}

public void setFollows1(List<RelationalJPAFollow> follows1) {
this.follows1 = follows1;
}

public RelationalJPAFollow addFollows1(RelationalJPAFollow follows1) {
getFollows1().add(follows1);
follows1.setUser1(this);

return follows1;
}

public RelationalJPAFollow removeFollows1(RelationalJPAFollow follows1) {
getFollows1().remove(follows1);
follows1.setUser1(null);

return follows1;
}

public List<RelationalJPAFollow> getFollows2() {
return this.follows2;
}

public void setFollows2(List<RelationalJPAFollow> follows2) {
this.follows2 = follows2;
}

public RelationalJPAFollow addFollows2(RelationalJPAFollow follows2) {
getFollows2().add(follows2);
follows2.setUser2(this);

return follows2;
}

public RelationalJPAFollow removeFollows2(RelationalJPAFollow follows2) {
getFollows2().remove(follows2);
follows2.setUser2(null);

return follows2;
}

public List<RelationalJPALog> getLogs() {
return this.logs;
}

public void setLogs(List<RelationalJPALog> logs) {
this.logs = logs;
}

public RelationalJPALog addLog(RelationalJPALog log) {
getLogs().add(log);
log.setUser(this);

return log;
}

public RelationalJPALog removeLog(RelationalJPALog log) {
getLogs().remove(log);
log.setUser(null);

return log;
}

public List<RelationalJPAPost> getPosts() {
return this.posts;
}

public void setPosts(List<RelationalJPAPost> posts) {
this.posts = posts;
}

public RelationalJPAPost addPost(RelationalJPAPost post) {
getPosts().add(post);
post.setUser(this);

return post;
}

public RelationalJPAPost removePost(RelationalJPAPost post) {
getPosts().remove(post);
post.setUser(null);

return post;
}

public String toString()
{
String postString = ("user: " + userID );
return postString;
}

}

这是我的 persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="LigiloBackendJPA" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.ligilo.server.storage.relational.mysql.RelationalJPAUser</class>
<class> ...
<properties>
<property name="openjpa.Log" value="DefaultLevel=WARN"/>
</properties>
</persistence-unit>
</persistence>

无论我做什么 - 它只是拒绝用它的 key 坚持它......有什么想法吗???

最佳答案

您似乎试图保留错误的对象。如果您想保存新用户,

mEm.persist(newUser);

关于java - org.apache.openjpa.persistence.PersistenceException : null keys not allowed (OpenJPA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19658184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com