我创建了一个模型,如下所示:
@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();
}
我尝试通过以下方式将其保存在 Controller 中:
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 | |
+-------------------+--------------+------+-----+---------+----------------+
我有以下问题:
- 我没有执行任何查询。我想 Play 内部就是这么做的。如何查看完整查询以及
save()
时正在执行的内容的值? - 如何解决这个问题?
最佳答案
How I can see full query with values of what is being executed?
在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。
How to solve this?
错误是因为current_time是mysql中的保留字。
解决方案
@Column(name="`current_time`") //backticks
public Date current_time = new Date();
它会成功。
关于java - Play 框架 - 保存模型时出现 Mysql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29009485/