I’m trying JPA with a very simple class for the Play! framework and I’m having some problems with the id column.
My sql database has only two columns:
CREATE TABLE IF NOT EXISTS `auto` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
And my model is:
@Entity
@Table(name = "auto")
public class Auto extends Model{
@Column(insertable = false, updatable = false)
public int id;
public String name;
public Auto(String name){
this.name = name;
}
}
Everything works fine without this part:
@Column(insertable = false, updatable = false)
public int id;
As soon as I add public int id; I’d get this error though: A JPA error occurred (Unable to build EntityManagerFactory): Repeated column in mapping for entity: models.Auto column: id (should be mapped with insert="false" update="false")
And that’s the reason I’ve added the column annotation, but it doesn’t work with that neither, now I’m getting:
A javax.persistence.PersistenceException has been caught, org.hibernate.PropertyAccessException: could not set a field value by reflection setter of models.Auto.id
I’m testing the model this way: new Auto("bmw").save(); save() is a method from the model class in the playframework.
Anyone know why I’m having this problem? Thanks!
Hmm, try it completely without the id field. Looks like Playframework auto-creates an Id field if extending the Model class. See here:
“…
If you have used JPA before, you know that every JPA entity must provide an @Id property. Here the Model superclass provides an automatically generated numeric ID, and in most cases this is good enough.
…”