user377628
user377628

Reputation:

Is it possible to automatically generate Hibernate mappings for Java classes?

I'd like to automatically generate Hibernate mappings for some Java classes I have. Basically, I'd like all objects in the class to have the equivalent of the @OneToOne annotation, and all collections to have the equivalent of the @OneToMany annotation by default. I'd like to be able to fine-tune this later, but 99% of my data works this way, and it would take a very long time to go through all the classes and add the annotations manually.

Is this possible?

edit: Sorry, I think omitted something very important: I'd actually like to generate these default annotations from classes, not from a database. I would still have to design about 40 tables if I were to start with the database, but I already have a package containing all those classes. Is it possible to generate the proper mappings based on existing Java classes?

Upvotes: 2

Views: 336

Answers (2)

Prateek Agrawal
Prateek Agrawal

Reputation: 1

If you are creating new classes then you can create the database structure first and then create classes using database. In eclipse, hibernate provides Hibernate code generation feature. You can use that.

Upvotes: 0

RBanerjee
RBanerjee

Reputation: 947

Yes, in eclipse there is a plugin called hibernate tools from jboss, which can generate mapping from a existing database.

You can generate all hibernate files both xml and annotations variant.

Just Google Hibernate Tools to know how to use it.

Thanks !!

Upvotes: 1

Related Questions