我正在使用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