Add constraint foreign key to entity in JPA
<p>I've an entity called <code>Profile</code>, whose primary key should actually be a foreign key pointing to a <code>RegisteredUser</code>.</p>

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

<pre><code>@Id
@OneToOne
@JoinColumn(foreignKey = @ForeignKey(name = "REGISTERED_USER_FK"))
private RegisteredUser registeredUser;
</code></pre>

<p>and in the <code>RegisteredUser</code> entity, I have the following:</p>

<pre><code>@OneToOne(cascade = CascadeType.ALL, mappedBy = "registeredUser")
private Profile profile;
</code></pre>

<p>Unfortunately when I run SpringBootApplication, I've the following error:</p>

<pre><code>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
</code></pre>

<p>when I try to run this code:</p>

<pre><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);
</code></pre>

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