Java org.apache.openjpa.persistence.PersistenceException:不允许使用空键(openjpa)
我用一个表创建了一个数据库:usersJava org.apache.openjpa.persistence.PersistenceException:不允许使用空键(openjpa),java,mysql,jpa,Java,Mysql,Jpa,我用一个表创建了一个数据库: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 EXIST
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;`
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();
}
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;
}
package com.ligilo.server.storage.relational.mysql;
进口。。。。
@实体
@表(name=“users”)
@NamedQuery(name=“RelationalJPAUser.findAll”,query=“从RelationalJPAUser r中选择r”)
公共类RelationalJPAUser实现可序列化的com.ligilo.server.storage.IUser
{
私有静态最终长serialVersionUID=1L;
@身份证
私有整数用户标识;
私人整数社区投票;
私有biginger-imageID;
个人浮动分数;
私有URL linkedInURL;
私有时间戳修改日期;
@时态(TemporalType.DATE)
私人日期签出日期;
@时态(TemporalType.DATE)
私人日期更新;
私有字符串用户描述;
私有字符串用户名;
私有字符串用户密码;
//双向多对一关联到关系关联
@OneToMany(mappedBy=“用户”)
私人名单附件;
//双向多对一关联到关系JPA注释
@OneToMany(mappedBy=“用户”)
私人名单评论;
//双向多对一关联到关系
@OneToMany(mappedBy=“user1”)
私人名单如下1;
//双向多对一关联到关系
@OneToMany(mappedBy=“user2”)
私人名单2;
//双向多对一关联到RelationalJPALog
@OneToMany(mappedBy=“用户”)
私有列表日志;
//双向多对一关联到关系
@OneToMany(mappedBy=“用户”)
私人名单员额;
公共关系{
}
公共整数getUserID(){
System.out.println(“获取用户ID”+userID.intValue());
返回this.userID;
}
public void setUserID(整数userID){
this.userID=userID;
System.out.println(“将用户id设置为”+userID.intValue());
}
公共整数getCommunityVotes(){
返回此。社区投票;
}
public void setCommunityVoces(整数CommunityVoces){
this.communitydoves=社区投票;
}
public BigInteger getImageID(){
返回此.imageID;
}
public void setImageID(BigInteger imageID){
this.imageID=imageID;
}
公共浮点数getImpactScore(){
返回此.impactScore;
}
公共无效设置影响分数(浮动影响分数){
this.impactScore=impactScore;
}
公共URL getLinkedInURL(){
返回this.linkedInURL;
}
public void setLinkedInURL(URL linkedInURL){
this.linkedInURL=linkedInURL;
}
公共时间戳getModifyDate(){
返回此.modifyDate;
}
public void setModifyDate(时间戳modifyDate){
this.modifyDate=modifyDate;
}
公共日期getSignOutDate(){
返回此.signOutDate;
}
public void setSignOutDate(日期signOutDate){
this.signOutDate=signOutDate;
}
公共日期getSignUpDate(){
返回此.signUpDate;
}
公共无效设置信号更新(日期信号更新){
this.signUpDate=signUpDate;
}
公共字符串getUserDescription(){
返回this.userDescription;
}
public void setUserDescription(字符串userDescription){
this.userDescription=userDescription;
}
公共字符串getUserName(){
返回此用户名;
}
public void setUserName(字符串用户名){
this.userName=用户名;
System.out.println(“将用户名设置为“+用户名”);
}
公共字符串getUserPassword(){
返回此.userPassword;
}
public void setUserPassword(字符串userPassword){
this.userPassword=userPassword;
}
公共列表getAttachments(){
返回此文件。附件;
}
公共附件(列出附件){
这个。附件=附件;
}
公共关系JPA附件添加附件(关系JPA附件){
getAttachments()。添加(附件);
附件.setUser(本文件);
返回附件;
}
公共关系JPA附件移除附件(关系JPA附件){
getAttachments()。删除(附件);
附件.setUser(空);
返回附件;
}
公共列表getComments(){
返回此条评论;
}
公共注释(列出注释){
this.comments=注释;
}
公共关系JPA注释添加注释(关系JPA注释注释){
getComments().add(comment);
comment.setUser(这个);
回复评论;
}
公共关系JPA注释删除注释(关系JPA注释注释){
getComments()。删除(注释);
comment.setUser(null);
回复评论;
}
公共列表getFollows1(){
返回此文件。follows1;
}
公共无效集合follows1(列表follows1){
this.follows1=follows1;
}
公共关系jpaFollowAddFollow1(关系jpaFollow1){
getFollows1().add(follows1);
follows1.setUser1(本);
返回follows1;
}
公共关系jpaFollow removeFollows1(关系jpaFollows1){
getFollows1()。删除(follows1);
follows1.setUser1(空);
返回follows1;
}
公共列表getFollows2(){
把这个还给我。follows2;
}
公共无效集合follows2(列表follows2){
this.follows2=follows2;
}
公共关系jpaFollow添加follows2(关系jpaFollows2){
getFollows2().add(follows2);
follows2.setUser2(本);
返回follows2;
}
公共关系jpaFollow removeFollows2(关系jpaFollows2){
getFollows2()。删除(follows2);
follows2.setUser2(空);
返回follows2;
}
公共列表getLogs(){
返回此.logs;
}
公共作废设置日志(列出日志){
this.logs=日志;
}
public RelationalJPALog addLog(RelationalJPALog日志){
getLogs().add(log);
log.setUser(这个);
返回日志;
}
public RelationalJPALog removeLog(RelationalJPALog日志){
getLogs()。删除(日志);
log.setUser(空);
返回日志;
}
公共列表getPosts(){
把这封信寄回去;
}
公共无效设置柱(列出柱){
这个.posts=posts;
}
公共关系jpapost addPost(关系
<?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>
mEm.persist(newUser);
<property name="openejb.jpa.auto-scan" value="true"/>