[play-framework] [play-1.2.5] Model class for existing table

283 views
Skip to first unread message

Gokul Pillai

unread,
Sep 18, 2012, 2:54:55 AM9/18/12
to play-fr...@googlegroups.com
I have a table in my MySQL database for which I am creating a new Model class extending from play.db.jpa.Model class.
Since the table already exists, I annotated the class mapping the table-name and primary key to an existing column:

@Entity
@Table(name="biz_companies")
public class BizCompany extends Model {
 
     @Id
     public long biz_id;

   
     public String name;
     public String street;
     public String state;
     public int zip;
     public String phone;
    
    public BizCompany() {
    }

    public BizCompany(long graphite_id, String name, String street,
            String state, int zip, String phone) {
        super();
        this.graphite_id = graphite_id;
        this.name = name;
        this.street = street;
        this.state = state;
        this.zip = zip;
        this.phone = phone;
    }
   
}

However, when I try to load some data from data.yml file for this table through a JUnit test, I get  an error.
javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of models.BizCompany.id
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:845)
    at play.db.jpa.JPABase._save(JPABase.java:31)
    at play.test.Fixtures.loadModels(Fixtures.java:221)

Any idea why this might be happening. If I remove the "@Id" annotation and run the test, it works fine but adds a new column to the table "id" which it assigns as the primary key.

I looked through the Play Model documentation but did not find an explanation. Any help would be appreciated.

Thanks

Marcel Klemenz

unread,
Sep 18, 2012, 3:01:43 AM9/18/12
to play-fr...@googlegroups.com
you set the biz_id the primary key, but Model.class has its own primary key called id;
You should extend GenericModel.class instead, then you can use your own pk

Manimaran Selvan

unread,
Sep 18, 2012, 3:05:14 AM9/18/12
to play-fr...@googlegroups.com
Or you can just use id as the variable and annotate it with @Column(name="biz_id")

Cheers,
Sent from BlackBerry® on Airtel

From: Marcel Klemenz <makl...@gmail.com>
Date: Tue, 18 Sep 2012 00:01:43 -0700 (PDT)
Subject: Re: [play-framework] [play-1.2.5] Model class for existing table
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/b_IgIeI-l6QJ.
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.

Gokul Pillai

unread,
Sep 19, 2012, 3:37:01 AM9/19/12
to play-fr...@googlegroups.com
Thanks, that worked.

--
Reply all
Reply to author
Forward
0 new messages