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 8099745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:30:44+00:00 2026-06-05T22:30:44+00:00

I was following Hibernate: Use a Base Class to Map Common Fields and openjpa

  • 0

I was following Hibernate: Use a Base Class to Map Common Fields and openjpa inheritance tutorial to put common columns like ID, lastModifiedDate etc in base table.

My annotated mappings are as follow :

BaseTable :

@MappedSuperclass
public abstract class BaseTable {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "lastmodifieddate")
    private Date lastModifiedDate;
...

Person table –

    @Entity
    @Table(name = "Person ")
    public class Person extends BaseTable  implements Serializable{

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

Create statement generated :

create table Person (id integer not null auto_increment,  lastmodifieddate datetime, name varchar(255), primary key (id)) ; 

After I save a Person object to db,

Person p = new Person();
p.setName("Test");
p.setLastModifiedDate(new Date());
..

getSession().save(p);

I am setting the date field but, it is saving the record with generated ID and LastModifiedDate = null, and Name=”Test”.

Insert Statement :

insert into Person (lastmodifieddate, name) values (?, ?)
binding parameter [1] as [TIMESTAMP] - <null>
binding parameter [2] as [VARCHAR] - Test

Read by ID query :
When I do hibernate query (get By ID) as below, It reads person by given ID.

Criteria c = getSession().createCriteria(Person.class);
c.add(Restrictions.eq("id", id));
Person person= c.list().get(0);
//person has generated ID, LastModifiedDate is null

select query select person0_.id as id8_,  person0_.lastmodifieddate as lastmodi8_, person0_.name as name8_ from Person person0_
 - Found [1] as column [id8_]
 - Found [null] as column [lastmodi8_]
 - Found [Test] as column [name8_ ]

ReadAll query :

Query query = getSession().createQuery("from " + Person.class.getName());
List<Person> allPersons=query.list();

Corresponding SQL for read all

select query select person0_.id as id8_,  person0_.lastmodifieddate as lastmodi8_, person0_.name as name8_ from Person person0_
- Found [1] as column [id8_]
- Found [null] as column [lastmodi8_]
- Found [Test] as column [name8_ ]
- Found [2] as column [id8_]
- Found [null] as column [lastmodi8_]
- Found [Test2] as column [name8_ ]

But when I print out the list in console, its being more weird. it is selecting List of Person object with

  • ID fields = all 0 (why all 0 ?)
  • LastModifiedDate = null
  • Name fields have valid values

I don’t know whats wrong here. Could you please look at it?

FYI,

My Hibernate-core version : 4.1.2, MySQL Connector version : 5.1.9 .

In summary, There are two issues here

  • Why I am getting All ID Fields =0 when using read all?
  • Why the LastModifiedDate is not being inserted?
  • 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-06-05T22:30:46+00:00Added an answer on June 5, 2026 at 10:30 pm

    Use

    @Temporal(TemporalType.DATE)  
    

    annotation on Date

    For ID generation use strategy.

    @GeneratedValue(strategy=GenerationType.AUTO) 
    

    or

    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    

    or using sequance

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "custom_generator")
    @SequenceGenerator(name = "custom_generator", sequenceName = "sequance_table")  
    

    also you can make property access in your child class:

    public abstract class BaseTable {
    
        protected int id;
    
        protected Date lastModifiedDate;  
    // ...  
    }    
    

    and

    @Entity
    @Table(name = "Person")
    public class Person extends BaseTable  implements Serializable{
    
    @Id
    @GeneratedValue
    @Column(name = "id")
    public getId()
    {
       return super.id;
    }
    
    setId(log id)
    {
       super.id = id;
    }
    
    @Column(name = "lastmodifieddate")
    @Temporal(TemporalType.DATE) 
    public getLastModifiedDate()
    {
       return super.lastModifiedDate;
    }
    
    setLastModifiedDate(Date date)
    {
       super.lastModifiedDate = date;
    }
    
    public getName()
    // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following setup A -> B -> C A-Mapping: <hibernate-mapping> <class name=db.base.A table=A>
I have made use of the following JPA implementations: Hibernate, Toplink, OpenJPA Each of
Dear all hibernate developers, The following hibernate hbm2java class I am using in my
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
I use Hibernate Envers for auditing. My entity looks like this: @Entity @Audited public
I want to convert the following subquery to use hibernate subquery: getCurrentSession().createQuery(from Employee where
I like to use Hibernate Search for implementing an sophisticated autosuggestion feature across multiple
trying to use hibernate with my web app and getting following exception: Initial SessionFactory
I am following Java Hibernate tutorial example from YouTube. Everything looks great until I
I'm trying to use Hibernate with GWT (using Gilead) but I got the following

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.