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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:13:09+00:00 2026-05-16T22:13:09+00:00

I am trying to create an Entity that has a byte[12] id in hibernate.

  • 0

I am trying to create an Entity that has a byte[12] id in hibernate. It seems like it does not like to have a byte[] as a primary key and as another column it sets it up as a tinyblob in the backing mysql db. I have tried to create a String, but the problem is that the string in java is 2 bytes per char, while it is one byte per char in mysql. I’m looking for something like this

@Entity
public class TestClass {
    @Id
    @Column(length=12)
    private byte[] id;
...

to map to a mysql table like this

Table 'testclass'
   Column id - varbinary     length 12     primary key
    ...

I have tried a number of different ways (primarily trying to fiddle with Strings) to do this but it does not seem to work right. Has anyone been able to do this already? Thanks.

  • 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-16T22:13:11+00:00Added an answer on May 16, 2026 at 10:13 pm

    As you experienced, this is not supported by standard JPA. From the specification:

    2.1.4 Primary Keys and Entity Identity

    …

    The primary key (or field or property
    of a composite primary key) should be
    one of the following types: any Java
    primitive type; any primitive wrapper
    type; java.lang.String;
    java.util.Date; java.sql.Date. In
    general, however, approximate numeric
    types (e.g., floating point types)
    should never be used in primary keys.
    Entities whose primary keys use types
    other than these will not be portable.
    If generated primary keys are
    used, only integral types will be
    portable. If java.util.Date is used as
    a primary key field or property, the
    temporal type should be specified as
    DATE.

    That being said, it appears that you could use a wrapper type around the byte[] and implement a custom user type. The UserType for a byte[] identifier property wiki page provides an implementation.

    Refer to the section 2.4.3.2. Type of the Hibernate Annotations documentation to declare and use this custom type with annotations.

    References

    • JPA 1.0 Specification
    • Section 2.1.4 "Primary Keys and Entity Identity"
    • Hibernate Annotations Reference Guide
    • 2.4.3.2. Type

    Resource

    • JBoss Community Wiki
    • UserType for a byte[] identifier property
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to declare a Primary Key for an Entity Model that will eventually
I'm trying to create an entity in my model that has two properties for
I am trying to create an application that has two UITableViews placed side-by-side. The
I have a C# workflow that I'm trying to create an IOrganzationService in that
I'm trying to parse a Tomboy note that has a link to another note
I have an 'Interview' entity that has a one-to-one mapping with a 'FormSubmission' entity,
I'm trying to use Dynamic Data with a entity model that has Abstract class
I am trying to extend the partial classes that the entity framework creates so
I am having a trouble while trying to create an entity with a custom
I am trying to create a new contact using Dynamic Entity. The sample i

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.