How can I persist Java domain model class objects into databases easily?

I have some Java domain model classes that I converted from XML using JAXB. Those are pretty complicated models. For example one class may have a list of the other class as his member.

Now I want to store those domain model class objects into relational database. Is hibernate a good choice for doing this persistence? Can I generate the database schema just based on the Java domain model classes with some tool?

When I store the object of the class that includes a list of another class, will the included class object persisted cascadingly?

FYI, I am giving a example of the domain model:
class A {
   List<B>  bs;
}

class B {
  List<C> cs;
}

class C {
   D d;
}

class D {
  ...
}

Open in new window

condor888Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim CakalicSenior Developer/ArchitectCommented:
>> Is hibernate a good choice for doing this persistence?
Probably. There are several free/open source JPA implementations that would appear to suit your needs. I don't see anything in the simple class structure you posted that would obviously preclude JPA (or Hibernate either directly or as a JPA EntityManager) as a solution. The A->B and B->C relationships are OneToMany mappings (one parent to many children). The lack of a reference to the parent in the child class (e.g., no reference to A from B) indicates you want the relationship to be navigable (directional) from A->B only (unidirectional relationships are easier to deal with and reason about than bidirectional relationships). Unclear whether the C->D relationship is a OneToOne or a ManyToOne. If what I just said sounds like a bunch of gobbledygook, you should spend some time with one or more JPA and/or Hibernate tutorials. For example:

>> Can I generate the database schema just based on the Java domain model classes with some tool?
Yes. Hibernate will do this at runtime using the annotations in your entity classes when the SessionFactory is created if the property hibernate.hbm2ddl.auto is set to update, create, or create-drop. There is also maven support for schema generation (I haven't used it):
If you are more familiar with writing DDL than annotations, It is also possibly to generate Java classes complete with annotations from your schema. Here's one guide on how to do that with Hibernate Tools in Eclipse:

>> When I store the object of the class that includes a list of another class, will the included class object persisted cascadingly?
As with most questions of this type, the answer is "that depends". To directly answer your concern: yes, persistence operations can (and usually should) be cascaded to relationships. This is called "persistence by reachability" There are various options for defining what operations cascade so you should become familiar with that part of the mapping. You can usually start off with cascade-all or cascade-all-delete-orphan and then fine tune from there. As you go through some of the tutorials there are plenty of such examples that will demonstrate cascading.

ORMs (doesn't matter which really) are a strange and wacky world that takes some getting used to. As with JDBC, there are varying implementations of the JPA specification. Developing to the spec aids portability though it won't preclude your needing to know details about your chosen JPA EntityManager (i.e., Hibernate) in the same way that programming to the JDBC spec doesn't preclude you from needing to know the connection URL syntax for your JDBC driver and probably some configuration options.

Regards,
Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
condor888Author Commented:
That's a great and in-depth answer. I should definitely look into the links you provided. Thanks a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.