Getting mapped class meta data

This post explains how to get hibernate related meta data information of a mapped class.

Class


package model;

public class Series
{
    private Integer id;
    private String name;
    private String description;

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }
}

Series.hbm.xml

<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”&gt;
<!– Generated Feb 23, 2014 7:59:16 PM by Hibernate Tools 3.4.0.CR1 –>
<hibernate-mapping>
<class name=”model.Series” table=”series”>
<id name=”id” type=”java.lang.Integer” column=”ID”>
<generator class=”assigned” />
</id>
<property name=”name” type=”java.lang.String” column=”NAME” />
<property name=”description” type=”java.lang.String” column=”DESCRIPTION” />
</class>
</hibernate-mapping>

Main


import java.util.Map;

import org.hibernate.SessionFactory;
import org.hibernate.metadata.ClassMetadata;

public class HibernateDemo2 {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.createSessionFactory();
        Map<String, ClassMetadata> map = sessionFactory.getAllClassMetadata();
        for(String className : map.keySet()) {
            ClassMetadata classMetadata = map.get(className);
            System.out.println(classMetadata.getEntityName());
            System.out.println(classMetadata.getIdentifierPropertyName());
            System.out.println(classMetadata.getVersionProperty());
            String[] propertyNames = classMetadata.getPropertyNames();
            for(String propertyName : propertyNames) {
                System.out.println(propertyName);
            }
        }
        HibernateUtil.shutdown();
    }
}

Output

model.Series
id
-66
name
description

Explanation

SessionFactory class’s getAllClassMetadata returns a map, where key is entity name and the value is classMetadata. Using the classMetadata instance you gather more information about the class using getter methods as shown in the above code.

1) We can get/set a property using classMetadata instance.
2) We can get a property type

etc. Refer to Hibernate 5.1 api for more information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s