2013-10-29 39 views
0
  • 我创建了一个数据库与一个表:用户org.apache.openjpa.persistence.PersistenceException:null键不允许(OpenJPA中)

    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:null键不允许从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> 

不管我做什么 - 它只是拒绝它坚持与其关键...任何想法?

+0

什么是'pObject'和'mEm'? –

+1

我们还需要看到你的'RelationalJPAUser'类。 – chrylis

+0

@CraigOtis mEm是一个'EntityManager'。我怀疑'pObject'是持续存在的错误。 – chrylis

回答

0

您是否在实体中将注释@Id标记为主键?你

可能还需要配置别的东西在你的persistence.xml

+0

是的,我用@Id标记主键 我将我的persistence.xml添加到问题。 – user2446947

1

你似乎是想坚持错了对象。如果你想保存你的新用户,

mEm.persist(newUser);