Keeping old database structure with Hibernate/JPA

195 views
Skip to first unread message

indrek

unread,
Jun 3, 2010, 3:47:05 PM6/3/10
to play-framework
This probably isn't a pure Play! question, but more of a Hibernate/JPA
question:
Is there a way to implement the models on an existing database
structure?


Currently if I create a model and save it to the database, the
existing database table structure gets overwritten. Any links on how
to achieve this would be very helpful!

Costis

unread,
Jun 3, 2010, 4:25:28 PM6/3/10
to play-framework
If you want to disable JPA automatic schema update set "jpa.ddl=none"
in application.conf

To reverse engineer an existing database into JPA I used Eclipse JPA
Tools. It was very easy. I used this guide: http://blog.platinumsolutions.com/node/221
Then I manually modified the generated classes to adapt them to Play's
Model (public fields instead of getters/setters, extending Model,
tweaking JPA annotations etc).

There is a play module called "db" that is supposed to reverse
engineer a database into play models but I had no success with it. I
tried it with Mysql.

indrek

unread,
Jun 4, 2010, 5:48:25 AM6/4/10
to play-framework
Thanks, the jpa.ddl=none stopped the overwriting of table structure.

But now I still have a problem - JPA still wants to use the id field
for primary key, but my database structure has the id field names as
employee. I tried doing the following:
@Entity
@Table(name = "employee")
public class Employee extends Model {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Integer employee;

public String first_name;

public Employee(String first_name) {
this.first_name = first_name;
}
}


But it still gives me the error:
A play.db.jpa.JPASupport$JPAQueryException has been caught, Error
while executing query <strong>from models.Employee where first_name = ?
1</strong>: Unknown column 'employee0_.id' in 'field list'
In /test/EmployeeTest.java, line 16 :
Employee emp = Employee.find("first_name", "Indrek").first();

And it doesn't write the employee to the database with the line:
new Employee("Indrek").save();
This line is before the error line.

Claus Guttesen

unread,
Jun 4, 2010, 6:20:13 AM6/4/10
to play-fr...@googlegroups.com
> But now I still have a problem - JPA still wants to use the id field
> for primary key, but my database structure has the id field names as
> employee. I tried doing the following:
> @Entity
> @Table(name = "employee")
> public class Employee extends Model {

Tried the same but jpa-play uses/requires id so your option is to add
a (serial) id column. I did that with a postgresql-db.

--
regards
Claus

When lenity and cruelty play for a kingdom,
the gentler gamester is the soonest winner.

Shakespeare

twitter.com/kometen

Guillaume Bort

unread,
Jun 4, 2010, 6:23:20 AM6/4/10
to play-fr...@googlegroups.com
Or you can extends directly the JPASupport class that does not provide
any default @Id field.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

indrek

unread,
Jun 6, 2010, 12:34:39 PM6/6/10
to play-framework
Thanks a lot, this did the trick and everything worked without
complicated configuration!

On Jun 4, 1:23 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Or you can extends directly the JPASupport class that does not provide
> any default @Id field.
>
>
>
> On Fri, Jun 4, 2010 at 12:20 PM, Claus Guttesen <kome...@gmail.com> wrote:
> >> But now I still have a problem - JPA still wants to use the id field
> >> for primary key, but mydatabasestructure has the id field names as
Reply all
Reply to author
Forward
0 new messages