Mysql JPA一对一关系无法检索自动增量

Mysql JPA一对一关系无法检索自动增量,mysql,jpa,eclipselink,auto-increment,one-to-one,Mysql,Jpa,Eclipselink,Auto Increment,One To One,我有两个Mysql表用户和帝国, 使用者 和帝国 CREATE TABLE `empires` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `pid` int(10) unsigned NOT NULL, `name` varchar(45) CHARACTER SET utf8 NOT NULL, `notes` varchar(90) CHARACTER SET utf8 NOT NULL, `world` tinyint(3) unsi

我有两个Mysql表用户和帝国, 使用者

和帝国

CREATE TABLE `empires` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pid` int(10) unsigned NOT NULL,
`name` varchar(45) CHARACTER SET utf8 NOT NULL,
`notes` varchar(90) CHARACTER SET utf8 NOT NULL,
`world` tinyint(3) unsigned NOT NULL,
`island` smallint(5) DEFAULT NULL,
`population` int(10) unsigned DEFAULT '20',
`gold` decimal(20,0) DEFAULT '500',
`percent` decimal(9,5) DEFAULT '50.00000',
`logo` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`,`pid`),
KEY `name` (`name`),
KEY `world` (`world`),
KEY `FK_pid` (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我有这些实体:

package boddooo.entity;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name="users")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private int activated=1;
private String activationcode="";
private String country;
private String email;
private String password;

public User(){}
public User(int id,String email,String password,String country) {
    this.id=id;
    this.email=email;
    this.password=password;
    this.country=country;
}

public int getId() {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}

public int getActivated() {
    return this.activated;
}

public void setActivated(int activated) {
    this.activated = activated;
}

public String getActivationcode() {
    return this.activationcode;
}

public void setActivationcode(String activationcode) {
    this.activationcode = activationcode;
}

public String getCountry() {
    return this.country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

}

此函数用于将新对象插入数据库

public void createUser() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException{
    Context icontext=new InitialContext();
    ut=(UserTransaction)icontext.lookup("java:comp/UserTransaction");
    ut.begin();
    User user=new User();
    user.setEmail(email);
    user.setPassword(password);
    user.setCountry(country);
    em.persist(user);
    Empire emp=new Empire();
    emp.setName(empirename);
    emp.setNotes(empirenotes);
    emp.setLogo(empirelogo);
    emp.setWorld(worldid);
    emp.setUser(user);
    em.persist(emp);
    ut.commit();
}
这是一对一的关系 empires.pid=users.id 但是当我调用这个方法时,它会插入用户和帝国,但是帝国中的pid字段的值是0,而不是自动递增的值。
我错过什么了吗?请帮助

@PrimaryKeyJoinColumn表示使用的字段是此实体的主键,因此实际上是只读的。这主要用于跨多个表的实体。 @JoinColumn是您应该使用的,因为它表明指定的列是传统外键,并且您希望使用目标值来设置此字段

@OneToOne
@JoinColumn(name="pid")
private User user;
您应该将@JoinColumnname=fk_id与@OneToOne一起使用。详情请浏览

public void createUser() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException{
    Context icontext=new InitialContext();
    ut=(UserTransaction)icontext.lookup("java:comp/UserTransaction");
    ut.begin();
    User user=new User();
    user.setEmail(email);
    user.setPassword(password);
    user.setCountry(country);
    em.persist(user);
    Empire emp=new Empire();
    emp.setName(empirename);
    emp.setNotes(empirenotes);
    emp.setLogo(empirelogo);
    emp.setWorld(worldid);
    emp.setUser(user);
    em.persist(emp);
    ut.commit();
}
@OneToOne
@JoinColumn(name="pid")
private User user;