我正在使用play 2.3和搭售来坚持一个对象控制台显示查询但数据没有插入数据库。JPA执行查询但没有插入数据
@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)
伐木我可以看到
insert into confrence_group (active, createdTime, name, uid, user_id) values (1, '[SQL NULL of type 93]', 'akkk', '1474449665379l2urhrti496h0hoplm9cs55srj', 1)
试过了变化它g至本地查询
字符串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检查,没有例外也没有数据表中插入两种情况。代码工作较早,但我不知道是什么导致它的行为如此
任何人都可以帮忙吗?
编辑:试图创建新模型,但问题是一样的。
你检查数据库配置自动提交设置? –
无dint为autocommit设置任何db配置。你能告诉我如何设置,因为当我阅读时,没有必要在jpa中自动提交,也是工作ealier,但不是现在 – codegasmer
也许你在junit测试中执行这个查询? @Transactional完成后默认测试回滚事务。 – mateuszlo