Talk to an expert

January 10th, 2012

Extract primary key from hibernate entity

by in Technology

Extract primary key from hibernate entity

Here is a quick one, I will show you how to extract the primary key and the value from a hibernate entity.

It took me a while to look at the API and see what I need to use, but after swimming in the API it was a couple of lines that got it all done.


First thing that you need to do is load the entity

Parent parent= parentDao.getEntityById(1L);

Once you load the entity call the following methods (code for the methods will follow)

System.out.println("print primary key: " +  HibUtil.extractPrimaryColumn(parent.getClass()) );
System.out.println("print primary key value: " +  HibUtil.extractPrimaryKeyValue(parent,Parent.class) );

Extract primary column name
Here is the code for the first method -> extractPrimaryColumn

Nothing hard about this. First, we load the PersistentClass based on our entity. If all the hibernate mappings are set up correctly there shouldn’t be any problems.

Once we have the PC we then call this method to get the name of the primary key -> getIdentifierProperty().getName();

    public static String extractPrimaryColumn(Object entity)
    {
        PersistentClass localPC = extractPersistentClass(entity);

        //Get pk column and value
        String name = null;
        try
        {
            name = localPC.getIdentifierProperty().getName();
        }
        catch (PropertyNotFoundException pnfe)
        {
            logger.info("Hib util, property not found",pnfe);
        }
        catch (MappingException me)
        {
            logger.info("mapping not found",me);
        }
        catch (HibernateException he)
        {
            logger.info("hibernate exception........but why??",he);
        }
        catch (NullPointerException npe)
        {
            logger.info("we have a problem getting a value for this entity",npe);
        }

        return name;
    }

Extract the value
Second method uses almost the same logic -> extractPrimaryKeyValue

After we get the PC we call the following methiod to get the value of the key -> pkValue = (Long) localPC.getIdentifierProperty().getGetter(clazz).get(entity);

public static Long extractPrimaryKeyValue(Object entity, Class clazz)
    {
        PersistentClass localPC = extractPersistentClass(entity);

        //Get pk column and value
        Long pkValue = null;
        try
        {
            pkValue = (Long) localPC.getIdentifierProperty().getGetter(clazz).get(entity);
        }
        catch (PropertyNotFoundException pnfe)
        {
            logger.info("Hib util, property not found",pnfe);
        }
        catch (MappingException me)
        {
            logger.info("mapping not found",me);
        }
        catch (HibernateException he)
        {
            logger.info("hibernate exception........but why??",he);
        }
        catch (NullPointerException npe)
        {
            logger.info("we have a problem getting a value for this entity",npe);
        }

        return pkValue;
    }

In my next post I will expend and show you how I use this code.

0 0 votes
Article Rating

January 10th, 2012

by in Technology

⟵ Back

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

0
Would love your thoughts, please comment.x
()
x