当我使用 GenerationType.IDENTITY 或 GenerationType.AUTO 作为我的表 ID 并且我在事务中保留一个实体时,会立即插入数据,并且在事务结束之前插入!!!
我尝试更改 GenerationType.TABLE,在我的 MYSQL 数据库中生成并执行新的关联 DDL,现在它可以工作了。有人可以解释我为什么吗?
提炼:
@Table
@Entity
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, unique = true)
private Long id;
...
}
@Repository
public class EntityDaoServiceImpl {
@PersistenceContext
protected EntityManager em;
@Transactional
public void testFooCreation() {
Foo foo = new Foo();
em.persit(foo);
//at this point with the AUTO or IDENTITY strategie,
//i ve got an effective insert in base otherwise with the TABLE strategie,
//the insert is effective at the end of my transaction (the excepted behavior)
}
}