【发布时间】:2014-04-26 16:15:01
【问题描述】:
我有啤酒
@Entity
@Table(name="beer")
public class Beer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String brewery;
private String name;
...
}
一瓶
@Entity
@Table(name="bottle")
public class Bottle {
@Id
@GeneratedValue
private Long id;
private BottleSize size;
@ManyToOne
private Beer beer;
...
}
还有道
public class BottleDao {
private EntityManager entityManager;
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void saveBottle(Bottle bottle) {
entityManager.persist(bottle);
}
public List<Bottle> findByBeer(Beer beer) {
Bottle foundBottle = entityManager.find(Bottle.class, beer);
return null;
}
public Bottle findById(Long id) {
Bottle foundBottle = entityManager.find(Bottle.class, id);
return foundBottle;
}
}
如果我跑了
@Test
public void testFindByBeer() throws Exception {
Beer pils = createHoepfnerPilsner();
beerDao.saveBeer(pils);
Beer hefe = createHoepfnerHefe();
beerDao.saveBeer(hefe);
Bottle bottlePils = createBottle().withBeer(pils).create();
bottleDao.saveBottle(bottlePils);
bottleDao.saveBottle(createBottle().withBeer(hefe).create());
flushAndClear();
List<Bottle> bottles = bottleDao.findByBeer(pils);
assertThat(bottles.size(), is(1));
assertThat(bottles.get(0).getId(), is(bottlePils.getId()));
}
我收到错误 java.lang.IllegalArgumentException:为类 Bottle 提供了错误类型的 id。预期:类 java.lang.Long,得到类 Beer
我错过了什么?
【问题讨论】:
标签: java hibernate annotations dao entitymanager