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

  • SEARCH
  • Home
  • 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 403759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:14:46+00:00 2026-05-12T17:14:46+00:00

Still duking it out with Hibernate… I’m using Hibernate-Annotations instead of hbm.xml files for

  • 0

Still duking it out with Hibernate…

I’m using Hibernate-Annotations instead of hbm.xml files for my beans, but I’m currently running into a problem where the SQL that Hibernate is generating references nonexistent database columns.

For instance, here is the code:

Query q = session.createQuery("FROM Status ORDER BY post_date DESC");

(it is retrieving a list of Status objects, ordered from most recent to least recent, and each Status object contains its own list of Comment objects…yes, think Facebook posts)

And here is the query it generates:

Hibernate: select status0_.pid as pid1_, status0_.content as content1_, status0_.owner_uid as owner5_1_, status0_.parent_pid as parent6_1_, status0_.post_date as post4_1_, status0_.type as type1_ from POSTS status0_ order by post_date DESC limit ?

The problem is, within that query, it references status0_.owner_uid and status0_.parent_pid, but those fields do not exist in the database. When I change the query manually to use status0_.owner and status0_.parent, respectively, and feed it to MySQL, it works perfectly.

There are four classes involved.

A User, which has no concept of anything else in the system (relevant fields below):

@Entity
@Embeddable
@Table(name="USERS")
public class User {

    @Id
    @GeneratedValue
    @Column(name="uid")
    private Integer uid;

...
}

A Post, an abstract superclass for Comment and Status that are stored in the same table and differentiated via a column type (relevant fields below):

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="type", 
    discriminatorType=DiscriminatorType.STRING
) 
@MappedSuperclass
@Entity
@Embeddable
@Table(name="POSTS")
public abstract class Post {

    @Id
    @GeneratedValue
    @Column(name="pid")
    private Integer pid;

    @ManyToOne
    @Embedded
    private Post parent;

    @ManyToOne
    @Embedded
    private User owner;

    @Column(name="post_date")
    private Timestamp postDate;

    @Column(name="content")
    private String content;

    @Column(name="type", updatable=false)
    private String type;
    ...
}

A Status class, subclassing Post; sets parent to null and also contains a list of Comments (relevant fields below):

@Entity
@Table(name="POSTS")
@DiscriminatorValue("status")
public class Status extends Post {

    @OneToMany(mappedBy="parent")
    @OrderBy("postDate asc")
    private List<Comment> children; 
    ...
}

A Comment class, subclassing Post; has a non-null parent (entire class posted below):

@Entity
@DiscriminatorValue("comment")
@Table(name="shannonq_posts")
public class Comment extends Post {
    // this class is literally empty
}

In summary: I have no idea why Hibernate is appending the embedded class’ IDs into the query, resulting in a non-existent column. Ideally I would have liked to have added the @Column(name="parent") annotation to these fields, but it seems that Hibernate doesn’t allow this particular annotation to fields labeled with @ManyToOne.

Any help is appreciated! Thank you!

Edit: FYI, if I manually change the columns in my database to match what Hibernate is generating, I get another error: Cannot instantiate abstract class or interface: Post. Obviously my configuration is incorrect. 😛

  • 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-12T17:14:46+00:00Added an answer on May 12, 2026 at 5:14 pm

    Hibernate’s default naming strategy for ManyToOne associations takes your field name and appends PK column of the associated table using underscore.

    Thus for Post (superclass of Status) you get owner_uid and parent_pid as column names because uid and pid are respective PK column names.

    You can override this by using @JoinColumn annotation:

    @ManyToOne
    @JoinColumn(name = "parent")
    private Post parent;
    
    @ManyToOne
    @JoinColumn(name = "owner")
    private User owner;
    

    or by implementing your own NamingStrategy (Hibernate-specific and not recommended for beginners 🙂 )

    On a side note, @Embedded signifies that you want to map given entity as component (basically, part of this same table). Putting @ManyToOne and @Embedded on the same property is (or should be) illegal.

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

Sidebar

Related Questions

been trying out this for quite some time but I'm still unable to built
The scenario is that we are copying files out to our drop folders, but
I've been looking over this post, but still can't figure out how to solve
Still learning magento coding. I wonder is there a way I can print out
still new to XML parsing with the iphone so i have a few questions.
Still an iphone dev starter but trying. I would like to have the user
I tried to research this, but there were still some questions left unanswered. I
Im using Preview 2 of the ASP .NET MVC Framework. Im trying out DataAnnotation
Code: $testing=sniper this and sniper that; $connectors=array('and', 'now', 'but', 'still', 'so', 'only', 'therefore', 'moreover',
OK there are many simple session questions out there, but I can't seem to

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.