nbro
nbro

Reputation: 15878

Add constraint foreign key to entity in JPA

I've an entity called Profile, whose primary key should actually be a foreign key pointing to a RegisteredUser.

Currently, this is how I'm trying to say that a field in Profile is both its primary and foreign key referencing RegisteredUser:

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

and in the RegisteredUser entity, I have the following:

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

Unfortunately when I run SpringBootApplication, I've the following error:

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

when I try to run this code:

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);

I've checked to see if the username of user is null, but it isn't...The problem is when I save rUser...

Upvotes: 1

Views: 3111

Answers (1)

WeMakeSoftware
WeMakeSoftware

Reputation: 9162

According to the JPA spec, you cannot annotate RegisteredUser with @Id annotation.

I cannot imagine why you need to separate Profile from User, but if you really need to do so you have several options:

Option A:

Use @Embeddable annotation on Profile. You won't get a separate table for Profile entity, but you will get a separate object in Java.

Option B:

Use @ManyToOne on Profile. So the lifecycle of user will be something like this:

  • Create user
  • Create profile
  • Save profile
  • Set user's profile to saved profile
  • Save user

You can even have the same primary keys on those objects (look for suggestions here)

Option C:

Configure @OneToOne, but forget about the requirement of having both entities having the same primary key.

Upvotes: 1

Related Questions