【问题标题】:JPA executes the query but data not insertedJPA 执行查询但未插入数据
【发布时间】:2017-01-29 11:16:06
【问题描述】:

我正在使用 play 2.3 并绑定一个对象,控制台显示查询,但数据未插入到数据库中。

@Entity
@Table(name = "confrence_group")
public class ConfrenceGroup {
    @Id
    @Column(name = "id", length = 50, unique = true, nullable = false, insertable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Constraints.Required
    @Column(columnDefinition = "varchar(25) default ''")
    private String name;

    @Column(columnDefinition = "int default 0")
    private int active = 1;

    @JsonIgnore
    @ManyToOne
    private User user;

    @Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:yy")
    @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP")
    private Date createdTime;

    @Column(unique = true)
    private String uid = (new UniqueToken()).nextSessionId();
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="javax.persistence.sql-load-script-source"
                      value="META-INF/sql/data.sql"/>
        </properties>

    </persistence-unit>

</persistence>

坚持我正在做的事情

JPA.em().persist(confGroupObjectReference)

从 Logging 我可以看到

insert into confrence_group (active, createdTime, name, uid, user_id) values (1, '[SQL NULL of type 93]', 'akkk', '1474449665379l2urhrti496h0hoplm9cs55srj', 1)

尝试将其更改为原生查询

String sql = "插入 confrence_group (active, name, uid, user_id) 值 (?, ?, ?, ?)";

   try {
        JPA.em().createNativeQuery(sql)
                .setParameter(1, 1)
                .setParameter(2, confo.getName())
                .setParameter(3, confo.getUid())
                .setParameter(4, user_id)
                .executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
    }

日志

 insert into confrence_group (`active`, `name`, `uid`, `user_id`) values (1, 'akkk', '1474443561160ojedp25m5k01e5k1kc7jg39l6p', 1)

但是对于这两种情况,当我检查我的数据库 gui 时,数据都没有插入表中,也不例外。代码之前工作过,但我不知道是什么导致它表现得像这样

有人可以帮忙吗?

编辑:尝试创建新模型,但问题相同。

【问题讨论】:

  • 您是否检查了数据库配置中的自动提交设置?
  • 没有为自动提交设置任何数据库配置。你能告诉我如何设置吗,因为当我阅读时,不需要在 jpa 中自动提交,而且它之前也可以工作,但现在不行
  • 也许你在 junit 测试中执行这个查询? @Transactional 测试完成后默认回滚事务。
  • @mateuszlo 不,我不是从 junit 运行它
  • 交易在哪里?

标签: java mysql jpa playframework playframework-2.3


【解决方案1】:

似乎你的 id 生成是由 Mysql 自动生成的,所以你需要 将@GeneratedValue(strategy = GenerationType.AUTO) 更改为@GeneratedValue(strategy=GenerationType.IDENTITY)

【讨论】:

  • 还是一样的输出
  • 你确定你的 'id' 列是 auto_increment 吗?
  • 并且可能 user_id 1 需要存在
  • 是的,因为我有我,它之前工作过,是的 user_id 存在
  • 所以显然你的事务没有提交。在您的方法上使用@Transactional(来自 play.db.jpa.Transactional 包)并查看:[playframework.com/documentation/2.5.x/JavaJPA]
猜你喜欢
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多