[英]JPA executes the query but data not inserted
I am using play 2.3 and tying to persist an object the console shows the query but data is not inserted in db. 我正在使用play 2.3并绑定持久对象,控制台会显示查询,但数据未插入db。
@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 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>
to persist I am doing 坚持我在做
JPA.em().persist(confGroupObjectReference)
From Logging I can see 从记录中我可以看到
insert into confrence_group (active, createdTime, name, uid, user_id) values (1, '[SQL NULL of type 93]', 'akkk', '1474449665379l2urhrti496h0hoplm9cs55srj', 1)
Tried changing it to native query 试图将其更改为本地查询
String sql = "insert into confrence_group ( active
, name
, uid
, user_id
) values (?, ?, ?, ?)"; 字符串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();
}
Logs 日志
insert into confrence_group (`active`, `name`, `uid`, `user_id`) values (1, 'akkk', '1474443561160ojedp25m5k01e5k1kc7jg39l6p', 1)
But for both cases data not inserted in table as I am checking through my database gui and there is no exception also. 但是对于这两种情况,数据都没有插入表中,因为我正在通过数据库gui检查,也没有例外。 The code was working earlier but I dont know what caused it to behave like this
该代码之前工作正常,但我不知道是什么导致了它的行为
Can anybody help? 有人可以帮忙吗?
EDIT: tried creating new model but the problem is same. 编辑:尝试创建新模型,但问题是相同的。
看来您的ID生成是Mysql自动生成的,因此您需要将@GeneratedValue(strategy = GenerationType.AUTO)
更改为@GeneratedValue(strategy=GenerationType.IDENTITY)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.