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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:14:39+00:00 2026-06-13T23:14:39+00:00

I have an unknown JPA entity and need to know it’s version. I could

  • 0

I have an unknown JPA entity and need to know it’s version. I could not find a way to do this generically.

I tried the metamodel, but don’t know what to pass to getVersion() method:

Object entity = ...;
Metamodel metamodel = entityManager.getMetamodel();
IdentifiableType<?> metaClass = 
    (IdentifiableType<?>)metamodel.managedType(entity.getClass());
metaClass.getVersion(<what to put here?>);

Same pattern is used in metaClass.getId(), but there is a complementary method getIdType() — getVersionType() is missing.

Secondly, there is entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity) method, but not getVersion(entity) method.

Is there some way to get version from an unknown entity?

  • 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-13T23:14:40+00:00Added an answer on June 13, 2026 at 11:14 pm

    I solved it in the following way: The javax.persistence.Version class specifies supported datatypes, only seven are supported, so I try all of them one after other. The JPA spec apparently misses the getVersionType() method, I think it is a bug.

    /**
     * Finds the version attribute for ManagedType instance.
     * @param managedType The metaobject
     * @return The version attribute or null, if it does not have one
     * @throws UnsupportedOperationException
     * If it has a version attribute, but is not one of supported types, as specified in {@link Version} documentation
     * (currently int, Integer, short, Short, long, Long, Timestamp).
     */
    private static <T> SingularAttribute<? super T, ?> findVersionAttribute(ManagedType<T> managedType) {
        if ( !(managedType instanceof IdentifiableType))
            return null;
    
        IdentifiableType<T> identifiableType = (IdentifiableType<T>)managedType;
        if ( ! identifiableType.hasVersionAttribute())
            return null;
    
        try {
            return identifiableType.getVersion(int.class);
        } catch (IllegalArgumentException e) {
            try {
                return identifiableType.getVersion(Integer.class);
            } catch (IllegalArgumentException e1) {
                try {
                    return identifiableType.getVersion(long.class);
                } catch (IllegalArgumentException e2) {
                    try {
                        return identifiableType.getVersion(Long.class);
                    } catch (IllegalArgumentException e3) {
                        try {
                            return identifiableType.getVersion(short.class);
                        } catch (IllegalArgumentException e4) {
                            try {
                                return identifiableType.getVersion(Short.class);
                            } catch (IllegalArgumentException e5) {
                                try {
                                    return identifiableType.getVersion(Timestamp.class);
                                } catch (IllegalArgumentException e6) {
                                    throw new UnsupportedOperationException("The version attribute is not of supported type");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for taking the time to read this. I have an unknown number of
Please forgive me if this is an obvious one. I have an unknown amount
I have a variable of unknown value, it will be an integer. For this
I have unknown number of objects in 3D Space, I would like to know
I have the following JPA (2.0.2) entities: Employee @Entity @Table(name = T_EMPLOYEE) public class
I have an unknown number of lines of input. I know that each line
I've a window that will have an unknown amount of text fields, determined by
I have numerous functions (unknown at design time) that each take a specific number
I have a dictionary of unknown depth and structure. It might contain more dictionaries,
I have a graph which contains an unknown number of disconnected subgraphs. What's a

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.