Create Java stub from Hibernate ORM file - java

Is there any jar which which will help me to create Java files from an existing hibernate orm-xml file?
Like we have an avro utility to convert (avsc) to .java files.
We have orm files and want to move toward Entity classes with annotations rather than using the orm files.
I tried converting xml- xsd and then using jaxb over it but failed.

JPA has no generator, there is no way of doing this.
It is never a good idea to mix data transfer objects (DTOs) together.
Your SOAP XML generated classes should only be used for the external API (SOAP DTOs).
Your application model should manage the persistence separately (JPA DTOs).
Doing it this way allows you to later change the API and the application model independently of each other.
It also allows you to easily add another API, e.g. REST and use REST DTOs for that (typically JSON).

Related

Need a way to generate Java classes for Spring Boot

I’m trying to build an administration portal accessible via web starting from the informations inside a database using Spring Boot with MyBatis for accessing the database. I wanted to find a way to reuse the code that I’m building for the portal with any other type of database, so I was wondering if there was a way to automatically generate classes for my project starting from the informations in the database, for example table names and fields...
Thanks in advance!
Telosys code generator (http://www.telosys.org) does this kind of job.
It uses the database schema to create a lightweight model that is used to generate the code (Java or any other language).
For more information see : https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
Everything is Open Source (tool and templates)
An option could be Jassd, which generates POJOs, Dao interfaces, and Dao implementation classes from a database. Use what you want from the output and discard the rest, the aim of this project was to not have to manually write getters and setters and have something to start from.
See the documentation and sample output at https://github.com/aforslund/jassd
(Note: I am the author, implemented this library to be able to get plain old Java objects generated and bootstrap som Dao/DaoImplementation classes using plain SQL and JDBCTemplate with Spring Boot).
If it is to switch db and reuse the code, simply changing these values in application.properties should help. If you have MySQL in development environment and postgres in production, that can also be handled. Check the following link for more details
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html
You are lucky! I just release cgV19 at https://github.com/carstenSpraener/cgV19
With it you could implement a model loader which connects to your database and reads the table information. It than provides this information to cgV19 as a meta model.
the next step is to implement a "cartridge" for cgV19 where you can generate java classes from that meta model.
if you try it out please give me feedback on you progress.
If you are going to use mybatis then MyBatis Generator is your best bet:
MyBatis Generator (MBG) is a code generator for MyBatis MyBatis and iBATIS. It will generate code for all versions of MyBatis, and versions of iBATIS after version 2.2.0. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s).
MyBatis Generator will generate:
Java POJOs that match the table structure.
MyBatis/iBATIS Compatible SQL Map XML Files. MBG generates SQL for simple CRUD functions on each table in a configuration.
Java client classes that make appropriate use of the above objects.

can i use jaxb with hibernate hbm2ddl?

I am using jaxb to autogenerate java classes from an xsd file. I will need to persist the data that will be stored in the objects that will be instantiated from the classes. Is there some way that the hibernate code can be autogenerated in the same classes that are autogenerated by JAXB?
I will need to regenerate the classes many times in the course of development. If I have to write the hibernate code by hand, the only reasonable method I can imagine is to write separate classes with hibernate for persistence, and to write connector classes that migrate the autogenerated classes into the persistence classes. Otherwise, my hand-written hibernate code would be over-written every time I re-run jaxb based on fine tuning of the xsd file. If hibernate code were also autogenerated, I could end up using a lot fewer classes.
Hyperjaxb3 is the way to go. It is a JAXB plugin that you include in the build process. When you run your xsd file through xjc, you will not only get xml-related annotations on the generated classes, you will also get JPA annotations. Without writing hardly any code, you will be able to take an XML document, persist it to a database, query the document from the database and get XML text back out. The generated code can be customized either in the xsd file or in an associated binding file (just like with vanilla xjc). We also utilize hbm2ddl on the produced classes to configure hibernate.
We have been actively using this on several schemas for the past couple of years.
As you edit your XML schema/bindings (and thus your DB schema), you will have to manually write a SQL migration script to upgrade any existing databases. It seems like most DBMS have a schema comparison tool that can be leveraged here. We automatically compare the freshly created schema to the migrated (from a baseline) schema on every build.

Is it possible to auto generate annotated-mapped POJO classes using techniques like Hibernate, JIBX etc?

Hibernate can auto-generate schemas from properly annotated POJO classes. And I also know that JIBX can create a data model (set of classes) out of properly structured XML schemas. Is there a way to automaticaly generate annotated-mapped classes from an XML schema? Or is it just possible to run a tool on a set of POJO classes, and expect it to create meaningful annotations on the specified classes? So later on we can create database schemas using these classes. To annotate every class that JIBX produces takes actually more work than manually designing the database schema according to the xml schema.
The Hyperjaxb project will generate JAXB classes from an XML schema that contain JPA annotations that could be used to create a database schema.
http://java.net/projects/hyperjaxb
Nice question! We had the same problem and we ended up developing the POJO generator with Freemarker.
By the way, the requirements to these POJOs may strongly vary, so, if such tool exists, it must have quite bloated configuration.

How do I get the original object back from a class generated by NetBeans for transfer by JAX-WS?

Java EE 6, NetBeans 6.9.1.
Part of my project is a SOAPy web service. I've written the server-side part of it, and that seems to work OK because GlassFish 3.0.1 is correctly generating WSDL files for the web services.
The web service sends and receives JPA entity classes, which the client operates on. I used NetBeans' "New > Web Service Client" wizard to generate the source code for the resource classes that represent the entities when they get sent by the web service — these resources have the same fields as the entity classes, getters and setters for those fields, and certain annotations from javax.xml.bind.annotation (e.g., #XmlAccessorType, #XmlType, #XmlElement), but they lack the other methods, etc. of the entity classes.
Once my web service client receives these resources, how do I get back the proper entity objects? I'd rather not manually reconstruct every entity, considering how many classes I have and how complicated their relationships are, this would be a lot of work.
Thank you! :)
P.S. I'm not sure what these resources classes are called; if anyone knows, it would help me write a more descriptive title.
One option would be to use a JAXB and JPA combo with something like HyperJAXB 3. Have a look at:
Relational storage using JAXB, JPA and HyperJAXB
Building JAX-WS, JAXB and JPA-based web service with Apache CXF, Spring and Hyperjaxb3
From WSDL to JAXB to JPA with a single schema: Adventures in Hyperjaxb3
Or using a JAXB implementation providing JPA support through extensions like MOXy:
JPA Entities to XML - Bidirectional Relationships (and Blaise's blog in general)
Creating a RESTful Web Service - Part 1/5
EclipseLink/Examples/MOXy/JPA
You need to convert your XML Schema classes to your entity manually by writing some converter classes

Which web service stack allows binding wsdl first web service to existing classes in Java?

Greetings,
I have a complicated scenario to handle. I have a wsdl file which uses a particular XML schema.
The XML schema is actually a handcrafted implementation of a specification. There is also a Java based implementation of the same specification. So the XSD used in WSDL and Java classes at hand are quite similar, but not exactly same.
Almost all web service stacks allow creating classes from WSDL or creating WSDL from Java class annotations.
What I want to do, is to use the WSDL and bind XSD used in the wsdl to existing java classes.
Should/can I do this by manually replacing generated Java classes with existing ones? Is it a matter of changing type names in config files and moving binding annotations to existing classes?
If you know any best practices, or java web service stacks that support this kind if flexibility in a practical way, your response would be much appreciated.
Best Regards
Seref
I suggest Spring's Web Services module, which has no code generation involved, but provides a clean separation of concerns. Different concerns are broken out nicely by allowing you to provide your WSDL and existing schema(s) on one side (contract first), your existing Java-based domain model on the other, and a way to plugin in your OXM (Object-XML Mapping) technology of choice.
Since you have hand-crafted WSDL/schema and hand-crafted Java classes, the real work will be in configuring your OXM. I prefer JiBX as it keeps the concerns separated (no XML annotation garbage mixed into your domain) with JAXB as a backup if the learning curve looks too steep. Spring Web Services supports several other OXM frameworks, and you can even use several different ones at once.
As far as best-practices, I consider hand-crafted code a best practice, though I may be in the minority. If you generate classes from XML you end up with classes that are simple data containers with no behavior (assuming you want to regenerate them whenever your WSDL/XSD changes). This is bad if you favor the object-oriented paradigm because you end up having to place your "business logic" in utilities/helpers/services etc. instead of in the domain objects where it really belongs. This is one reason I favor JiBX. I can make very nice OO objects with behavior, a nice clean schema that doesn't necessarily match the objects, and can manage changes to either side with a mapping file similar to how hibernate does it for ORM (Object-Relational Mapping). You can do the same with JAXB, but that requires embedding XML structure into your object model, and binds a single XML representation to it (whereas with JiBX you can have many).
MOXY (I'm the tech lead) was designed for instances where you have an existing XML Schema and an exsting object model. It accomplishes this through XPath based mapping and can ever handle cases where the models are not that similar:
parse google geocode with xstream
MOXy also has an external binding file:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
MOXy is a JAXB implementation with extensions (A couple of which are mentioned above). IF you go ahead with Spring, MOXy is configured as a JAXB implementation, and you need to add a jaxb.properties file in with your model classes with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Categories

Resources