Please use the google group to ask questions - thanks.

by Landei 07 Jun 15:54
Autogenereated @Id in Scala

It seems I can't convince the @Id column to autogenerate keys. I have a H2 database in server mode with a fairly simple table:

CREATE TABLE AWAY.Users (
ID INT PRIMARY KEY,
NAME VARCHAR(255),
PWDHASH INT
)

But whatever I try, EBean wants to persist only the name and the pwdhash, and "forgets" the id:
Exception in thread "main" javax.persistence.PersistenceException: org.h2.jdbc.JdbcSQLException: NULL not allowed for column ID; SQL statement:
insert into Away.Users (pwdhash, name) values (?, ?) [90006-114]

Here is my Scala class:

@Entity
@Table{val name="Away.Users"}
@BeanProperty //with and without (it generated getters and setters)
class User {
@Id
@GeneratedValue //with and without
var id:java.lang.Integer = 0 //or with Int
var name:String = ""
var pwdhash:Int = 0
}

The id column gets persisted when I don't have an @Id on it, but of course it doesn't it genereates new ones, so I get a duplicate key for the second user...

Any ideas???

Thanks,
Landei

07 Jun 16:03
by Landei

Oh, I forgot the calling code:

val ebeanServer = Ebean.getServer(null);
val user = ebeanServer.createEntityBean(classOf[User]).asInstanceOf[User]
user.name = "Landei"
user.pwdhash = "secret".hashCode
ebeanServer.save(user)

08 Jun 06:15
by Landei

OK, I'm stupid. Just used autoincrement in the table.

Create a New Topic

Title:
Body:
 
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse