0

I have a Java Bean class which has some attributes having some Object type as datatype. Again that Object is having attributes which is again have some Object datatype and so on...

public class MyService   {
  private String id;
  private String href;
  private Category category;
}

public class Category {
  private String id;
  private Type type;
}

public class Type {
  private String id;
  private SomeObject someObject;
}

so on ...

Now I want to create JPA Entity classes for the class MyService with all the Mappings (OneToMany etc.). I know from database table we can generate the Entity classes but can we do it from the Java bean classes?

Because if I do manually it will take much time. So please suggest if I can generate those Entity classes or some other alternative ways instead of manually creating those Entity classes.

Surya
  • 604
  • 1
  • 6
  • 25
  • I'm not 100% sure, but as far as I know, there's no way for you to generate an entity with all the correct database relations between them... You could tough, create a script that auto generates the model for you, but you still would have to manually set the relations. – Lucas Feb 15 '22 at 13:11
  • https://www.jpa-buddy.com/ – Simon Martinelli Feb 15 '22 at 13:23
  • What exactly do you want generated? If you specify with annotations or ORM.xml which of these classes are entities, JPA can handle most of the relationship and mappings for you, and even generate the database from it. Just add in @ Entity and @ Id on each class - or write a tool that somehow picks up your Classes and auto adds them and their id property to an orm.xml file. But to be honest, if it takes that much time, your object model might not be right for a database store - you have to understand and analyze it or you are bound to run into problems with any serialization/storage mechanism – Chris Feb 15 '22 at 17:21
  • What you need is an object mapping technology Refer this https://stackoverflow.com/questions/1432764/any-tool-for-java-object-to-object-mapping . – Tomz Mar 03 '22 at 17:22

0 Answers0