Imagine that I have a Debtor
class. With Hibernate, I will define the class like that:
@Entity
@Table(name = "T_DEBTOR")
public class Debtor {
@Id
@Column(name = "ID_DEBTOR")
private String idDebtor;
...
My DAO will then looks like:
public class DebtorDaoImpl implements DebtorDao {
@PersistenceContext
private EntityManager em;
@SuppressWarnings("unchecked")
public List<Debtor> findAllDebtors() {
Query q = em.createQuery("select d from Debtor d");
return (List<Debtor>) q.getResultList();
}
This works well. However, I am in a configuration where I need to access differents schemas (as pointed here). Of course, in each schema the table that hosts the debtor list has not the same name. In addition to that, they may not have the exact same structure. That why I have x differents Debtor
class (where x is the number of schemas I manipulate).
In the case where I have two differents schemas, I will have two different Debtor
class: DebtorOne
and DebtorTwo
.
As I want to ease my developments, I created an Interface (or an Abstract class, it does not change my problem here) that is implemented by both DebtorOne
and DebtorTwo
:
public interface Debtor {
String getIdDebtor();
}
and:
@Entity
@Table(name = "T_DEBTOR_ONE")
public class DebtorOne implements Debtor {
@Id
@Column(name = "ID_DEBTOR")
private String idDebtor;
...
If I let my DAO as it is, I get the following error from Hibernate:
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Debtor is not mapped [select d from Debtor d]
If I change my DAO to have this:
public List<Debtor> findAllDebtors() {
Query q = em.createQuery("select d from DebtorOne d");
return (List<Debtor>) q.getResultList();
}
then it works, but it is specific to DebtorOne
schema...
One solution I see is to define a named query on the DebtorOne
and DebtorTwo
classes and call this named query from my DAO.
In others words:
@Entity
@Table(name = "T_DEBTOR_ONE")
@NamedNativeQueries( { @NamedNativeQuery(name = "findAllDebtors", query = "select d from DebtorOne d") })
public class DebtorOne implements Debtor {
and in the DAO:
@SuppressWarnings("unchecked")
public List<Debtor> findAllDebtors() {
Query q = em.createNamedQuery("findAllDebtors");
return (List<Debtor>) q.getResultList();
}
I didn't try it yet, but I think it will work...
EDIT I just tried, this will work... except that the NamedQuery
must be named differently for DebtorOne
and DebtorTwo
...
However, I am wondering if there is a way to solve my problem without using the latter solution?
Edit regarding the first answer, which suggests to use the @MappedSuperclass
. This annotation seems to be the perfect solution for me, but I think I forgot something, as I still get the same error.
The main Debtor
:
@MappedSuperclass
public class Debtor {
@Id
@Column(name = "IDDEBTOR")
protected String idDebtor; // With getter and setter
}
one of the extended Debtor
class:
@Entity
@Table(name = "DEBTOR_ONE")
public class DebtorOne extends Debtor {
...
and in my DAO:
public List<Debtor> findAllDebtors() {
return (List<Debtor>) em.createQuery("select d from Debtor d").getResultList();
}
is still returning me the error Caused by: org.hibernate.hql.ast.QuerySyntaxException: Debtor is not mapped [select d from Debtor d]
What did I miss this time?