0

Am Using Ebeans with Play2.0.2 I have two Models like this :

@Entity
@Table(name="GRP_MST")
public class GroupMst extends Model {

@Id
@Constraints.Required
@Formats.NonEmpty
public String groupid;
......
@OneToMany(cascade = CascadeType.ALL)
public List<DtlMenuGrp> dtlMenuGrpList; // This one is giving problems 

}

And my DtlMenuGrp Model looks like:

@Entity
@Table(name="DTL_MENU_GRP")
public class DtlMenuGrp extends Model {

@Constraints.Required
@Formats.NonEmpty
public String groupid;
....
}

I want to Join the above two Models so in My controller :

GroupMst groupMst = GroupMst.find.where().eq("groupid",data.get("grpid")).findUnique();
List<DtlMenuGrp> dtlMenuGrpList = groupMst.dtlMenuGrpList;

But this gives me below Excpetion of column not found :

Caused by: javax.persistence.PersistenceException: Query threw SQLException:Invalid column name 'group_mst_groupid'. 
Bind values:[1000] 
Query was:
select t0.groupid c0
        , t1.groupid c1, t1.rightid c2, t1.status c3, t1.createdby c4, t1.createdon c5 
from GRP_MST t0
left outer join DTL_MENU_GRP t1 on t1.group_mst_groupid = t0.groupid  
where t0.groupid = ?   
order by t0.groupid 

Why does my column is casted as group_mst_groupid instead of groupid

Suave Nti
  • 3,721
  • 11
  • 54
  • 78
  • How did you create 1.sql evolution? did you wrote it yourself or created automatic with Ebean ? – biesior Aug 08 '12 at 15:01

1 Answers1

1

It cannot be casted as gruopid as your DtlMenuGrp also contains such field (nota bene didn't you forget @id annotation?)

There are some rules on which Ebean creates fields keys for associations, and group_mst_groupid fits in it as it's combination of the model name and its @Id field. I just tested how it will generate the 1.sql with models' definitions you gave and it created exactly like this:

create table DTL_MENU_GRP (
group_mst_groupid         varchar(255) not null,
groupid                   varchar(255))
;

what's more I have not any error about that.

My advice, try to change model names to avoid using enigmatic abbreviations in their names, always try to find possible shortest single word for models and fields. Try to avoid overriding table names with annotation until it's really required. Then you'll be able to find errors much faster and if required to fix them manually...

ie. Instead GroupMst(table GRP_MST) create a Group model with id field

@Entity
public class Group extends Model {

  @Id
  public Long id; //why not a numeric id? for an example Long ?

  @OneToMany(cascade = CascadeType.ALL)
  public List<Menu> menus;
  // ...


}

Just guessing...

@Entity
public class Menu extends Model {

  @Id
  public Long id;
  // ...

}

Then, use Ebean for generating whole DDL from the scratch, and you'll be amazed how simple and logical it is...

biesior
  • 55,576
  • 10
  • 125
  • 182
  • +1..Thanks for the explanation..quite new to `ebean` and `ORM`..I dont know about `1.sql` evolution..have manually created Models for my existing DB structure.. – Suave Nti Aug 08 '12 at 18:40
  • I'd suggest to get familiar with automatic created 1.sql file even on quite separate project. And really - try to simplify models and fields names, you'll see in the future why it's good idea, especially, when you'll disable automatic DDL and start writing it from the scratch. – biesior Aug 08 '12 at 19:12
  • Problem is the present project which I am working under has already existing Database, so I have disabled automatic DDL ..any suggestions for this situation? – Suave Nti Aug 09 '12 at 05:54