2

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?

2 Answers 2

1

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 !!

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

1 Comment

To generate hb mappings hb tools do reverse engineering, so you need to provide the logic that what relationship the classes have. Ànd even if you had the hb classes generated, there is nothing nothing wrong to regenerate it and replace.
0

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.

1 Comment

Agreed, at some point of time you will have to create the tables if you do that in advance you will easily create the classes with mapping and shifting the classes from one package to another is not a big deal.

Your Answer

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