Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 443703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:11:50+00:00 2026-05-12T21:11:50+00:00

Imagine that I have a Debtor class. With Hibernate, I will define the class

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:11:51+00:00Added an answer on May 12, 2026 at 9:11 pm

    I think this is not possible with an interface, but only with a common abstract base class, that will be annotated with @MappedSuperclass (see Hibernate documentation for further details)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.