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

AKFA
  • 170
  • 11

1 Answers1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you! I was able to get all the field properties. For getting field size, the following comment was helpful: http://stackoverflow.com/questions/1816780/how-to-reuse-fieldlength-in-form-validation-and-ddl/1946901#1946901 – AKFA Mar 28 '11 at 12:34