NPE with @JoinColumn and Play2 and Ebean

658 views Asked by At

I have the following property in my entity :

@OneToOne
@JoinColumn(unique = true)
@NotNull(message = ValidatorUtils.ERROR_NOT_NULL)
public User user;

and I get the following stacktrace when I refresh my browser, while trying to generate database migration :

[error] application - 

! @6gmdbcgi0 - Internal server error, for (GET) [/] ->

play.api.UnexpectedException: Unexpected exception[NullPointerException: null]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:148) ~[play_2.10.jar:2.2.0]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.0]
    at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.0]
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.0]
    at scala.util.Success.flatMap(Try.scala:200) ~[scala-library.jar:na]
Caused by: java.lang.NullPointerException: null
    at com.avaje.ebeaninternal.server.ddl.CreateTableVisitor.isDbColumnWritten(CreateTableVisitor.java:59) ~[avaje-ebeanorm.jar:na]
    at com.avaje.ebeaninternal.server.ddl.CreateTableColumnVisitor.visitOneImported(CreateTableColumnVisitor.java:111) ~[avaje-ebeanorm.jar:na]
    at com.avaje.ebeaninternal.server.ddl.VisitorUtil.visit(VisitorUtil.java:109) ~[avaje-ebeanorm.jar:na]
    at com.avaje.ebeaninternal.server.ddl.VisitorUtil.visit(VisitorUtil.java:73) ~[avaje-ebeanorm.jar:na]
    at com.avaje.ebeaninternal.server.ddl.VisitorUtil.visitBean(VisitorUtil.java:62) ~[avaje-ebeanorm.jar:na]
    at com.avaje.ebeaninternal.server.ddl.VisitorUtil.visit(VisitorUtil.java:36) ~[avaje-ebeanorm.jar:na]

If I remove the @JoinColumn annotation, my schema is migrated correctly. How can I specify the column to be unique without having this error ? Thanks.

EDIT 01/09/2014 : Add generated ddl

Here is the generated ddl :

As we can see there is a foreign key for user

I'd like the generated ddl to contain something like : constraint uq_user_id unique (user_id)

1

There are 1 answers

9
jrook On

Instead of @JoinColumn(unique = true) try: @Column(unique=true)

Also, if you want to make a combination of multiple columns unique, you can use: Table(name="table_name",uniqueConstraints=@UniqueConstraint(columnNames={"column1","column2"}))