我有这些实体:
@Entity(name = "PersonneSingleTableDo")
@Table(name = "personne")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "sexe", discriminatorType = DiscriminatorType.STRING)
public abstract class PersonneDo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "nom")
private String nom;
@Column(name = "datenaissance")
private Date datenaissance;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "personneDo", fetch = FetchType.EAGER)
private Set<AmitieDo> amis;
...
@Entity(name = "HommeSingleTableDo")
@DiscriminatorValue(value = "homme")
public class HommeDo extends PersonneDo {
}
@Entity(name = "FemmeSingleTableDo")
//@Table(name = "personne")
@DiscriminatorValue(value = "femme")
public class FemmeDo extends PersonneDo {
}
@Entity(name = "AmitieSingleTableDo")
@Table(name = "amis")
public class AmitieDo {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name = "idPersonne")
private PersonneDo personneDo;
@ManyToOne
@JoinColumn(name = "idAmi")
private PersonneDo amiPersonneDo;
...
我有两个Daos管理我的2个继承类:
@Repository("femmeSingleTableJpaDao")
@Transactional(value = "singletable", propagation = Propagation.MANDATORY)
public class FemmeSingleTableJpaDao implements IFemmeSingleTableDao {
@PersistenceContext(unitName = "springmvcapp_pu_singletable")
private EntityManager entityManager;
/* (non-Javadoc)
* @see ili.persistence.dao.IFemmeSingleTableDao#createFemmeDo(ili.persistence.bean.stratsingletable.FemmeDo)
*/
@Override
public void createFemmeDo(FemmeDo femmeDo) {
entityManager.persist(femmeDo);
}
/* (non-Javadoc)
* @see ili.persistence.dao.IFemmeSingleTableDao#findFemmeDo(int)
*/
@Override
public FemmeDo findFemmeDo(int idFemmeDo) {
final FemmeDo femmeDo = entityManager.find(FemmeDo.class, idFemmeDo);
return femmeDo;
}
/* (non-Javadoc)
* @see ili.persistence.dao.IFemmeSingleTableDao#findAllFemmeDo()
*/
@Override
public List<FemmeDo> findAllFemmeDo() {
final TypedQuery<FemmeDo> query = entityManager.createQuery("select p from FemmeDo p",
FemmeDo.class);
return query.getResultList();
}
/* (non-Javadoc)
* @see ili.persistence.dao.IFemmeSingleTableDao#updateFemmeDo(int, ili.persistence.bean.stratsingletable.FemmeDo)
*/
@Override
public void updateFemmeDo(int idFemmeDo, FemmeDo femmeDo) {
femmeDo.setId(idFemmeDo);
entityManager.merge(femmeDo);
}
/* (non-Javadoc)
* @see ili.persistence.dao.IFemmeSingleTableDao#deleteFemmeDo(int)
*/
@Override
public void deleteFemmeDo(int idFemmeDo) {
entityManager.remove(entityManager.getReference(FemmeDo.class, idFemmeDo));
}
}
@Repository("hommeSingleTableJpaDao")
@Transactional(value = "singletable", propagation = Propagation.MANDATORY)
public class HommeSingleTableJpaDao implements IHommeSingleTableDao {
@PersistenceContext(unitName = "springmvcapp_pu_singletable")
private EntityManager entityManager;
/* (non-Javadoc)
* @see ili.persistence.dao.IPersonneDao#createPersonneDo(ili.persistence.bean.PersonneDo)
*/
@Override
public void createHommeDo(HommeDo hommeDo) {
entityManager.persist(hommeDo);
}
/* (non-Javadoc)
* @see ili.persistence.dao.IPersonneDao#findPersonneDo(int)
*/
@Override
public HommeDo findHommeDo(int idHommeDo) {
// final TypedQuery<PersonneDo> query = entityManager.createQuery(
// "select p from PersonneDo p where p.id = :id", PersonneDo.class);
// query.setParameter("id", idPersonneDo);
// return query.getSingleResult();
final HommeDo hommeDo = entityManager.find(HommeDo.class, idHommeDo);
return hommeDo;
}
/* (non-Javadoc)
* @see ili.persistence.dao.IPersonneDao#findAllPersonneDo()
*/
@Override
public List<HommeDo> findAllHommeDo() {
final TypedQuery<HommeDo> query = entityManager.createQuery("select p from HommeDo p",
HommeDo.class);
return query.getResultList();
}
/* (non-Javadoc)
* @see ili.persistence.dao.IPersonneDao#updatePersonneDo(int, ili.persistence.bean.PersonneDo)
*/
@Override
public void updateHommeDo(int idPersonneDo, HommeDo personneDo) {
personneDo.setId(idPersonneDo);
entityManager.merge(personneDo);
}
/* (non-Javadoc)
* @see ili.persistence.dao.IPersonneDao#deletePersonneDo(int)
*/
@Override
public void deleteHommeDo(int idHommeDo) {
// final Query query = entityManager.createQuery("delete from PersonneDo p where p.id = :id");
// query.setParameter("id", idPersonneDo);
// query.executeUpdate();
entityManager.remove(entityManager.getReference(HommeDo.class, idHommeDo));
}
}
我有一个测试所有方法的JUnit testCase:持久化,查找和更新工作正常,但我有两个问题:
首先,在Dao上,findAll方法都不起作用。它输出这些错误:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: HommeDo is not mapped [select p from HommeDo p]
and
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: FemmeDo is not mapped [select p from FemmeDo p]
其次,当我删除一个与另一个朋友('amis')的人('Homme')并且我检索(再次找到)这个第二个人时,它仍然将第一个人列为朋友,尽管有级联注释:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "personneDo", fetch = FetchType.EAGER)
你能帮助我理解为什么并解决这两个问题吗?
答案 0 :(得分:0)
对@Table
和HommeDo
FemmeDo
@Table(name = "table_x")
另外,如果您不使用name = "HommeSingleTableDo"
注释中的@Entity
,请将其删除。例如:
@Entity