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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:54:58+00:00 2026-05-26T17:54:58+00:00

I’m pretty new with hibernate, and I’m trying to transform a JDBC project I

  • 0

I’m pretty new with hibernate, and I’m trying to transform a JDBC project I have into Hibernate.

I’m using annotations, and I managed to annotate the basic stuff, however, I’m stuck now with the more heavy objects, I don’t know how to annotate them.
Here’s the Class:

@Entity
@Table(name = "person")
public class Person {

    public Person{

    }

    // THIS WILL BE SOON INJECTED BY SPRING
    private static transient PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    private static transient EmailValidator validator = EmailValidator.getInstance();

    @Id
    @Column(name = "person_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "private_name", nullable = false, length = 20)
    private String privateName;

    @Column(name = "middle_name", length = 20)
    private String middleName;

    @Column(name = "family_name", nullable = false, length = 20)
    private String familyName;

    @Column(name = "age", nullable = false)
    private int age;

    @Column(name = "address1", nullable = false)
    private String address1;

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

    //How do I annotate this ? --> Google LIBPHONENUMBER

    private PhoneNumber phone;

    // How do I annotate this ? --> This is a normal PNG image file.
    private File image;

Edit:
The File was previously mapped as a BLOB.
The PhoneNumber was previously persisted as String, and was transformed using the PhoneNumber constructor to Phonenumber.

  • 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-26T17:54:58+00:00Added an answer on May 26, 2026 at 5:54 pm

    The other comments about using @Lob are correct for the File type. It is also correct that if you can change the schema to not save the file data in the DB, then you probably should.

    To map your PhoneNumber class to a database field, you’re going to need to use a Hibernate custom UserType. It basically tells Hibernate HOW to do the object<–>db mapping for classes that it doesn’t already know about. Telling the PhoneNumber field in Person to use a custom user type is easy:

    @Type(type = PhoneNumberType.CLASS_NAME)
    @Column
    private PhoneNumber phone;
    

    This assumes a very simple one-column storage of the phone number.

    To write PhoneNumberType, you’ll need to implement UserType. It looks overwhelming, with the assemble/disassemble/deepCopy, but the main part you care about is nullSetGet/Set, returnedClass and sqlTypes. You’ll end up with some code like this inside your custom type:

    @Override
    public Class<?> returnedClass() {
        return PhoneNumber.class;
    }
    
    @Override
    public int[] sqlTypes() {
        return new int[] { Types.VARCHAR };
    }
    
    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        final String value = rs.getString(names[0]);
        return /* PhoneNumber instance created from string. */
    }
    
    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.VARBINARY);
            return;
        }
    
        st.setString(index, ((PhoneNumber) value).toString());
    }
    

    You can find plenty of information about how to implement the other methods via google, stackoverflow and the hibernate javadocs. It isn’t that hard to do.

    UPDATE: Multi-column user type

    Implement CompositeUserType instead of just UserType. There are a few method changes that you care about. First you’ll want to define the multiple property names and types:

    public String[] getPropertyNames() {
        return new String[] { "number", "code" };
    }
    
    public Type[] getPropertyTypes() {
        return new Type[] { StandardBasicTypes.STRING,
                            StandardBasicTypes.STRING };
    }
    

    There’s also getPropertyValue/setPropertyValue to implement. Your nullSafeXxxx implementations would change to read and write two properties instead of one:

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        // Access column in order defined in getPropertyNames()
        final String number = rs.getString(names[0]);
        final String code = rs.getString(names[1]);
        return /* PhoneNumber instance created from number and country code. */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.