1 つのテーブルを持つ DB を作成しました: 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: null keys not allowed throw from the 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>
私が何をしても-キーでそれを永続化することを拒否するだけです...何かアイデアはありますか????