Play Framework CRUD搜索问题

时间:2011-04-11 21:31:51

标签: mysql jpa playframework mysql-error-1054

在尝试搜索Play的CRUD模块中的某些实体时,我遇到了这个异常:

    play.exceptions.JavaExecutionException: org.hibernate.exception.SQLGrammarException: could not execute query
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:290)
    at Invocation.HTTP Request(Play!)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:250)
    at play.db.jpa.JPAPlugin$JPAModelLoader.fetch(JPAPlugin.java:431)
    at controllers.CRUD$ObjectType.findPage(CRUD.java:253)
    at controllers.CRUD.list(CRUD.java:36)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:413)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:408)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:182)
    ... 1 more
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2452)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
    at org.hibernate.loader.Loader.list(Loader.java:2187)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
    ... 7 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'CHARSET' in 'where clause'
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1869)
    at org.hibernate.loader.Loader.doQuery(Loader.java:718)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    ... 15 more

奇怪的是,搜索适用于某些实体,而不适用于其他实体。例如,对于以下实体,在搜索框中添加任何字符串都可以:

   @Entity
public class Act extends Model {

    @Transient
    private static final int PAGE_SIZE = Integer.parseInt(Play.configuration.getProperty("pagination.size","10"));

    @Required(message = "act.name.required")
    public String name;

    @Required(message = "act.description.required")
    @Lob
    @MaxSize(value=500, message="act.description.maxsize")
    public String description;

    public Blob image;

    public boolean showInClosedMode;

    @Temporal(TemporalType.TIMESTAMP)
    public Date updated;

    @Required(message = "act.theatre.required")
    @ManyToOne
    public Theatre theatre;

    public boolean disabled;

    @OneToMany(mappedBy="act")
    public List<Offer> offers;

    @ManyToMany(cascade=CascadeType.PERSIST)
    public Set<Tag> tags;
    @Transient
    public String tagListSupport;

    [... some methods ...]
    }

但不适合这个:

@Entity
public class User extends Model {

    @Required(message = "user.name.required")
    public String name;
    @Email(message = "user.email.invalid")
    public String email;
    public String prefLang;
    public Long prefCity;
    public int credits;
    public Date lastLogin;
    @NoBinding("profile")
    public boolean disabled;
    @NoBinding("profile")
    public boolean admin;
    @NoBinding("profile")
    public boolean anonymous;
    @OneToMany(mappedBy = "owner")
    public List<Ticket> tickets;
    @ManyToMany
    public List<Theatre> theatres;
    public String googleId;
    public String yahooId;
    public String facebookId;
    public String twitterId;
    public String twitter_token;
    public String twitter_secret;
    public String username;
    public String password;
    @OneToMany(mappedBy = "user")
    public List<Event> history;

    [...Methods...]
}

知道为什么会这样吗?

2 个答案:

答案 0 :(得分:4)

我的猜测是你正在使用的数据库不喜欢你有一个名为“user”的表,这是你没有为你的表提供特定名称时得到的。我知道Postgres不允许“用户”表,因为“user”是一个关键字。我不确定MySQL。尝试在User类中的@Entity注释之后添加javax.persistence.Table注释:

@Entity
@Table(name = "my_user")
public class User extends Model {
    ...
}

你给它一个你想要的不是“用户”的名字。或者(但未经测试)您可以将“用户”名称包装在引号中:

@Entity
@Table(name = "\"user\"")
public class User extends Model {
    ...
}

答案 1 :(得分:1)

小心列名。

2012-08-08T12:58:29+00:00 app[web.1]: 12:58:29,513 ERROR ~ Unsuccessful: create table users (id int8 not null, email varchar(255), enabled bool not null, name varchar(255), user varchar(255), primary key (id))
2012-08-08T12:58:29+00:00 app[web.1]: 12:58:29,514 ERROR ~ ERROR: syntax error at or near "user"

这就是我的情况,更改专栏&#34; user&#34; to&#34; userId&#34;解决了我的问题