java - Play 框架 - 保存模型时出现 Mysql 错误

标签 java mysql datetime playframework timestamp

我创建了一个模型,如下所示:

@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    |                |
+-------------------+--------------+------+-----+---------+----------------+

我有以下问题:

  1. 我没有执行任何查询。我想 Play 内部就是这么做的。如何查看完整查询以及 save() 时正在执行的内容的值?
  2. 如何解决这个问题?

最佳答案

How I can see full query with values of what is being executed?

  1. application.conf中添加

    db.default.logStatements=true

  2. 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。

Source

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/

相关文章:

java - 将来自多个输入的数据保存到文件

java - swagger-codegen-maven-plugin 在生成 API 类时忽略我为 ZonedDateTime 设置的导入映射

php - 来自数据库的数据与 htmlentities 相呼应,在希伯来语中给出了奇怪的迹象

mysql - 我如何从另一台服务器的另一个数据库中获取数据?

c# 从滴答中重现日期时间

python - 格式化 timedelta 对象

java - 是否可以将 Spring Boot 应用程序嵌套在另一个 Spring Boot 应用程序中?

java - 如何让我的网络服务器等待?

mysql WHERE SET 数据类型包含项目

php - 如何使用 php $row 检索 sql datetime 对象?