【发布时间】:2013-11-01 11:44:15
【问题描述】:
当我使用 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)
}
}
【问题讨论】:
-
i ve got an effective insert in base你是怎么检查的? (在em.persist();之后添加一个虚拟System.out.println("Breakpoint");。在此处设置一个断点并使用外部工具查看您的表 Foo 以查看新行是否真的在您的数据库中似乎是一个好方法。你做了吗那个?)