0
我是Spring和JPA的新手,遇到標題中指定的問題。爲了簡化問題,我有兩個類:User和FeedItem。用戶可以有更多的FeedItem,但關係是雙向的(FeedItem知道它與哪個用戶關聯)。他們都堅持在數據庫中使用JPA + Hibernate的:Spring + JPA「超出鎖定等待超時;嘗試重新啓動事務」
@Entity
@Table
public class User
{
@Id
@GeneratedValue
@Column(name = "id", nullable = false, length = 8)
private int id;
@Column(nullable = false, length = 32, unique = true)
private String alias;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<FeedItem> feedItems = new ArrayList<FeedItem>();
public User()
{
}
public User(String alias)
{
this.alias = alias;
}
... getters/setters...
}
@Entity
@Table
public class FeedItem
{
@Id
@GeneratedValue
@Column(name = "id", nullable = false, length = 16)
private int id;
@Column(nullable = false, length = 64)
private String title;
@ManyToOne
@JoinColumn(name = "userId", nullable = false)
private User user;
public FeedItem()
{
}
public FeedItem(String title, User user)
{
this.title = title;
this.user = user;
}
... getters/setters...
}
的DAO中:
@Repository
public class UserJpaDao implements UserDao
{
private EntityManager em;
@Transactional
public User save(User user)
{
return this.em.merge(user);
}
@Transactional
public void delete(User user)
{
this.em.remove(user);
}
@Transactional(readOnly = true)
public User findById(int id)
{
return this.em.find(User.class, id);
}
@PersistenceContext
void setEntityManager(EntityManager entityManager)
{
this.em = entityManager;
}
}
@Repository
public class FeedItemJpaDao implements FeedItemDao
{
private EntityManager em;
@Transactional
public FeedItem save(FeedItem feedItem)
{
return this.em.merge(feedItem);
}
@Transactional
public void delete(FeedItem feedItem)
{
this.em.remove(feedItem);
}
@Transactional
public FeedItem findById(int id)
{
return this.em.find(FeedItem.class, id);
}
@PersistenceContext
void setEntityManager(EntityManager entityManager)
{
this.em = entityManager;
}
}
這是測試給出錯誤:
@RunWith(SpringJUnit4ClassRunner.class)
public class FeedItemJpaDaoTest
{
@Autowired
private DriverManagerDataSource dataSource;
@Autowired
private FeedItemJpaDao feedItemDao;
@Autowired
private UserJpaDao userDao;
@Before
@Transactional
public void setUp() throws Exception
{
DatabaseOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
}
@After
@Transactional
public void tearDown() throws Exception
{
DatabaseOperation.DELETE_ALL.execute(getConnection(), getDataSet());
}
@Test
@Transactional
public void testSave() throws Exception
{
User user = userDao.findById(3);
FeedItem feedItem = new FeedItem("Achievement unlocked!", user);
feedItem = feedItemDao.save(feedItem);
assertEquals(feedItem, feedItemDao.findById(feedItem.getId()));
}
private IDatabaseConnection getConnection() throws Exception
{
return new DatabaseConnection(dataSource.getConnection());
}
private IDataSet getDataSet() throws Exception
{
return new FlatXmlDataSetBuilder().build(new File("src/test/resources/dataset.xml"));
}
}
我不明白爲什麼錯誤發生 - 任何建議表示讚賞!
謝謝。
編輯:好像這個問題是由於DbUnit的:如果我註釋掉的刪除()方法,誤差不occour
什麼是基礎數據庫你正在使用?這似乎是數據庫中的鎖定超時問題。 – 2012-02-03 09:48:01
@AbhinavSarkar MySQL – satoshi 2012-02-03 10:41:24
我編輯了主要問題,好像問題是由於DbUnit造成的! – satoshi 2012-02-03 10:48:05