1

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...

nbro
  • 15,395
  • 32
  • 113
  • 196
  • 1
    The column `registered_user_username` belongs to which entity, `RegisteredUser`? – António Ribeiro Apr 23 '16 at 18:44
  • Why do you separate Profile from User? Can Profile exist without User object? – WeMakeSoftware Apr 23 '16 at 18:45
  • @aribeiro There's no such a column. I've a field in `RegisteredUser` called `username` which also should be the primary key of `RegisteredUser` but should also be a foreign key referencing the primary key of the `User`... – nbro Apr 23 '16 at 18:45
  • @Funtik Indeed, it should be a weak entity whose primary key is a foreign key referencing a `RegisteredUser` – nbro Apr 23 '16 at 18:46
  • @nbro, then, on your database, the attribute `username` of your `RegisteredUser` is mapped to which column? Can you please update your question with the `RegisteredUser` entity? – António Ribeiro Apr 23 '16 at 18:49
  • 1
    MySQL clearly states you are trying to insert or update `registered_user_username` so if that column does not exist: Check your JPA model. – Norbert Apr 23 '16 at 18:56

1 Answers1

1

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.

Community
  • 1
  • 1
WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52