我创建的模型如下:
@Entity
@Table(name ="news_items")
public class NewsItem extends Model {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
public long id;
public String url;
public long friend_id;
public int count_for_the_day;
public String friend_name;
public String friend_img;
public Date friend_posted_at;
public String friend_text;
@Formats.DateTime(pattern="yyyy-MM-dd HH:mm:ss.SSS")
public Date current_time = new Date();
}
我试图通过以下方式将其保存在控制器中:
NewsItem _newsItem = new NewsItem();
_newsItem.url = shared_url;
_newsItem.friend_name = status.getUser().getName();
_newsItem.friend_img = status.getUser().getProfileImageURL();
_newsItem.friend_id = status.getUser().getId();
_newsItem.friend_text = status.getText();
_newsItem.friend_posted_at = status.getCreatedAt();
_newsItem.count_for_the_day = 1;
_newsItem.save();
resultItems.add(_newsItem);
这些值都不是null
。我在激活控制台中收到错误:
Caused by: javax.persistence.PersistenceException: ERROR executing DML bindLog[]
error[You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'current_time) values
('http://www.lahamag.com/Details/42019/119/%D8%B3%D8%B9%D9%' at line 1]
与mysql DB中的变量相关的部分表结构如下所示:
| friend_text | varchar(255) | YES | | NULL | |
| current_time | datetime | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
我有以下问题:
save()
时执行内容的值?答案 0 :(得分:1)
如何使用正在执行的内容的值查看完整查询?
在 application.conf 中添加
db.default.logStatements=true
在内容 conf 文件夹中创建 logger.xml 文件
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.jolbox.bonecp" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<logger name="play" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<logger name="application" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
这将显示在控制台中生成的sql。
如何解决这个问题?
错误是因为current_time是mysql中的保留字。
解决方案
@Column(name="`current_time`") //backticks
public Date current_time = new Date();
它会起作用。
答案 1 :(得分:0)
显然current_time
是MySQL中的保留字,应该引用,TBH我不知道如何在Ebean中强制使用这些引号,因此最快的解决方案是将字段重命名为某些非保留字,如publishDate
BTW,正如您所看到的,您可以在模型中使用 camelCase 名称,Ebean通过将查询更改为 underscored_version
来正确处理它们