在JPA中向实体添加约束外键

时间:2016-04-23 18:39:36

标签: java mysql spring hibernate jpa

我是一个名为Profile的实体,其主键实际上应该是指向RegisteredUser的外键。

目前,这就是我试图说Profile中的字段是其主要和外键引用RegisteredUser的方式:

@Id
@OneToOne
@JoinColumn(foreignKey = @ForeignKey(name = "REGISTERED_USER_FK"))
private RegisteredUser registeredUser;

RegisteredUser实体中,我有以下内容:

@OneToOne(cascade = CascadeType.ALL, mappedBy = "registeredUser")
private Profile profile;

不幸的是,当我运行SpringBootApplication时,我发现了以下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'registered_user_username' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_40]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_40]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.Util.getInstance(Util.java:387) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994) ~[mysql-connector-java-5.1.38.jar:5.1.38]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    ... 42 common frames omitted

当我尝试运行此代码时:

User user = new User("blah");
repo.save(user); // using repositories to store entities to the db

Profile profile = new Profile(user.getFirstName());

RegisteredUser rUser = new RegisteredUser(user.getUsername(), profile);
rur.save(rUser);

我已检查用户的用户名是否为空,但它不是......问题是我保存rUser ...

1 个答案:

答案 0 :(得分:1)

根据JPA spec,您无法使用RegisteredUser注释对@Id进行注释。

我无法想象为什么你需要将Profile与用户分开,但如果你真的需要这样做,你有几个选择:

选项A:

@Embeddable上使用Profile注释。您不会为Profile实体获得单独的表,但是您将获得Java中的单独对象。

选项B:

@ManyToOne上使用Profile。所以用户的生命周期将是这样的:

  • 创建用户
  • 创建个人资料
  • 保存个人资料
  • 将用户的个人资料设置为已保存的个人资料
  • 保存用户

您甚至可以在这些对象上使用相同的主键(查找建议here

选项C:

配置@OneToOne,但忘记了让两个实体具有相同主键的要求。