我正在使用播放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)
从记录中我可以看到
insert into confrence_group (active, createdTime, name, uid, user_id) values (1, '[SQL NULL of type 93]', 'akkk', '1474449665379l2urhrti496h0hoplm9cs55srj', 1)
尝试将其更改为原生查询
String sql =&#34;插入confrence_group(active
,name
,uid
,user_id
)值(?,?,?,?)&# 34 ;;
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进行检查,也没有例外。代码工作得比较早,但我不知道是什么导致它表现得像这样
有人可以帮忙吗?
编辑:尝试创建新模型,但问题是一样的。
答案 0 :(得分:0)
看来你的id生成是由Mysql自动生成的,所以你需要
将@GeneratedValue(strategy = GenerationType.AUTO)
更改为@GeneratedValue(strategy=GenerationType.IDENTITY)