views:

271

answers:

2

Is there any Utility available to convert XML based config files (say in Spring or Hibernate) to Annotations? If not would it be worthwhile to build such a tool?

+1  A: 

Hi,

Go to the following page

https://www.hibernate.org/hib_docs/tools/reference/en/html/ant.html

And see item 4.4.2.

POJO java code exporter (<hbm2java>)

It says:

<hbm2java> is a java codegenerator. Options for controlling wether JDK 5 syntax can be used and whether the POJO should be annotated with EJB3/Hibernate Annotations.

<hibernatetool destdir="${build.dir}/generated">
    <configuration configurationfile="hibernate.cfg.xml"/>
    <hbm2java jdk5="true" ejb3="true"/>
</hibernatetool>

About ejb3 attribute, it says:

Code will contain EJB 3 features, e.g. using annotations from javax.persistence and org.hibernate.annotations

Arthur Ronald F D Garcia
I don't think that's what Debu is asking. He wans to generate annotations on entities that would produce Hibernate configuration equivalent to the XML-based one he has. I strongly doubt SchemaExport can do that; but if it can I'd appreciate if you've pointed me out to a doc describing the process.
ChssPly76
@ChssPly76 You are right, Chss, I think Hibernate Tools is the only way to do it. UPvote to you. Thank you!
Arthur Ronald F D Garcia
+2  A: 

Hibernate Tools provide this kind of functionality.

They can read existing mappings using either XML files or annotations and subsequently export configuration to XML files or generate annotated POJOs.

Hibernatetool Ant task documentation has examples of how to do both.

That said, my experience with it (DB reverse engineering via older version) has been somewhat mixed; generated POJOs provde a good basis but have to be corrected by hand. This may or may not be the case for hbm-to-annotations translation, though.

ChssPly76