0

In my Hibernate + Spring application, I have several annotation based domain objects. I would like to go through all of them and create metatable with definitions for all of them. This metatable should look like this:

Entity Name |    Field Name |    Field Type |    Field Size|    etc.

For example,

@Entity
public class User {

@Column(name = "username", length = 255)
private String username;

@Column(name = "password", length = 255)
private String password;
}

should create two records

Entity Name |    Field Name |    Field Type |    Field Size|
User        |    username   |     String    |     255      |
User        |    password   |     String    |     255      |

How can be this done?

P.S. We are using Spring's LocalContainerEntityManagerFactoryBean for data access

1 Answer 1

1

You could use SessionFactory's getAllClassMetadata method, which would give you all you need except perhaps the field size (which I haven't found in the javadoc). But even that should be relatively easy to do by inspecting the @Column annotation set on the class field corresponding to each persistent property of the class.

From the ENtityManager, you can call getDelegate to get access to the Hibernate session, and then call getSessionFactory() to get access to the session factory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was able to get all the field properties. For getting field size, the following comment was helpful: stackoverflow.com/questions/1816780/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.