2011-04-11 88 views
4

試圖播放我得到這個異常的CRUD模塊中的一些實體搜索時:播放框架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...] 
} 

任何想法,爲什麼出現這種情況?

+0

你不具備產生不好的SQL查詢字符串?我可以看看代碼,從這個字符串中查看你的模型究竟有什麼問題...... – mandubian 2011-04-18 14:22:02

+0

關於生成的SQL的任何消息?無論如何,你可能會像我一樣熟練地分析代碼:)......這只是爲了幫助;) – mandubian 2011-04-19 12:06:12

+0

你應該真的發佈生成的SQL。 – Kawu 2011-04-21 13:59:46

回答

4

我的猜測是你正在使用的數據庫不喜歡你有一個名爲「user」的表,這是當你沒有爲你的表提供一個特定的名字時得到的。我知道Postgres不允許使用「用戶」表,因爲「用戶」是一個關鍵字。我不確定MySQL。嘗試在User類添加註釋javax.persistence.Table您@Entity標註後:

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

,你給它,你想要一個什麼名字,是不是「用戶」。或者(但未測試),您可能會將「用戶」名稱用引號括起來:

@Entity 
@Table(name = "\"user\"") 
public class User extends Model { 
    ... 
} 
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" 

這是我的情況下,改變列「用戶」到「用戶id」解決了我的問題