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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:02:02+00:00 2026-05-14T20:02:02+00:00

I write an application based on an already existing database (postgreSQL) using JPA and

  • 0

I write an application based on an already existing database (postgreSQL) using JPA and Hibernate. There is a int2-column (activeYN) in a table, which is used as a boolean (0 => false (inactive), not 0 => true (active)). In the Java application i want to use this attribute as a boolean. So i defined the attribute like this:

@Entity
public class ModelClass implements Serializable {    
  /*..... some Code .... */
  private boolean active; 

  @Column(name="activeYN")
  public boolean isActive() {
    return this.active; 
  }
 /* .... some other Code ... */

}

But there ist an exception because Hibernate expects an boolean database-field and not an int2.
Can i do this mapping i any way while using a boolean in java??

I have a possible solution for this, but i don’t really like it:
My “hacky”-solution is the following:

@Entity
public class ModelClass implements Serializable {    
  /*..... some Code .... */
  private short active_USED_BY_JPA; //short because i need int2

  /**
   * @Deprecated this method is only used by JPA. Use the method isActive()
   */
  @Column(name="activeYN")
  public short getActive_USED_BY_JPA() {
    return this.active_USED_BY_JPA;
  }

  /**
   * @Deprecated this method is only used by JPA. 
   * Use the method setActive(boolean active)
   */
  public void setActive_USED_BY_JPA(short active) {
    this.active_USED_BY_JPA = active; 
  }

  @Transient //jpa will ignore transient marked methods
  public boolean isActive() {
    return getActive_USED_BY_JPA() != 0; 
  }

  @Transient
  public void setActive(boolean active) {
    this.setActive_USED_BY_JPA = active ? -1 : 0;
  }

 /* .... some other Code ... */

}

Are there any other solutions for this problem?

The “hibernate.hbm2ddl.auto”-value in the hibernate configuration is set to “validate”.

(sorry, my english is not the best, i hope you understand it anyway)..

  • 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-14T20:02:02+00:00Added an answer on May 14, 2026 at 8:02 pm

    Well, the problem is that int2 is used store signed two-byte integer, regardless of how you use it and Hibernate doesn’t have any knowledge of your logic to translate the value into a boolean at the Java level.

    So you’ll have to use your hacky solution (i.e. read the int2 column in a short and convert it in your entity) that you could maybe make things a bit sexier by moving annotations on attributes:

    @Entity
    public class ModelClass implements Serializable {    
      /*..... some Code .... */
    
      @Column(name="activeYN")
      private short active; 
    
      public boolean isActive() {
        return this.active!=0; 
      }
     /* .... some other Code ... */
    
    }
    

    Or, if using a JPA extension is not a problem (JPA dosen’t support custom types), use a custom type that you declare with the @Type annotation:

    @Entity
    public class ModelClass implements Serializable {    
      /*..... some Code .... */
      private boolean active; 
    
    
      @org.hibernate.annotations.Type(type = "my.package.ShortToBooleanUserType")
      @Column(name="activeYN")
      public boolean isActive() {
        return this.active; 
      }
     /* .... some other Code ... */
    
    }
    

    Where ShortToBooleanUserType is a class that implements either org.hibernate.UserType or org.hibernate.CompositeUserType (refer to the documentation) and does the conversion in both direction.

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

Sidebar

Ask A Question

Stats

  • Questions 414k
  • Answers 414k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There you go: public Pair<Date, Date> getDateRange() { Date begining,… May 15, 2026 at 8:47 am
  • Editorial Team
    Editorial Team added an answer you can try something like this: i = find(in(1:end,2)>20 &… May 15, 2026 at 8:47 am
  • Editorial Team
    Editorial Team added an answer I think you should keep the controller for the following… May 15, 2026 at 8:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.