0
  • 我用一个表创建了一个数据库:用户

    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>

不管我做什么——它只是拒绝用它的关键来坚持它……有什么想法吗????

4

3 回答 3

1

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

mEm.persist(newUser);
于 2013-10-29T12:33:19.720 回答
0

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

此外,您可能需要在 persistence.xml 中配置其他内容

于 2013-10-29T12:32:13.547 回答
0

尝试将此行添加到您的 persistence.xml

<property name="openejb.jpa.auto-scan" value="true"/>

它扫描您的 jpa 注释实体类并将它们注册到 jpa 容器。

于 2020-01-21T14:27:33.557 回答