0

I am working with a project where I need to store a object in hibernate and it consist of many user defined objects. I know how use hibernate mapping with a object that only contain "ususal" types(int, String, etc...) but with custom types I've seen suggestions such as usage of the annotation @embedded and implementing UserType but I haven't seen any suggestion on how to simply map the objects inside the object to a certain table. Note: these objects aren't going in the same table, just in the same db. What I want to do is have a mapping that allows this function in my DaoImplementation:

public void store(MyObject o){
hibernateTemplate.saveOrUpdate(o);
}

This is soft of how my object looks:

public class MyObject{

private String name;

private ObjectA type;//Contains an int

private ObjectC look;//Contains a String.

private ObjectB[] children;//contains a string and other children.

public MyObject(){}

//Getters and setters omitted.

}

public class ObjectB{
private String name;
private ObjectB[] children;

public ObjectB(){}
//Getters setters omitted
}

2 Answers 2

1

If the contents of some object needs to be stored in a separate table, then this object should be another persistent entity, and you should use associations between entities : OneToMany, ManyToOne, OneToOne, ManyToMany, depending on the cardinalities.

Read the hibernate reference manual.

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

3 Comments

I've been trying this but there is one problem. Does the child have to have a field that stores the parent? Look at this example: mkyong.com/hibernate/hibernate-one-to-many-relationship-example It's looking good but the problem is that Stock has several StockDailyRecords but StockDailyRecord needs to have a Stock field. Looks ugly to me and in my current project I'm not allowed to add fields in classes. Any idea on a work around? Haven't seen anyone in the manual.
Children objects don't have to have a field with parent (of course, table they are mapped to still needs a column with parent id). Just don't specify the one-to-many relationship in parent class as inverse.
@why_vincent : what you want is a unidirectional OneToMany relationship. And the manual does have examples of such a relationship. Read it carefully.
0

You should write a normal entity mapping for class ObjectB (same way you're mapping MyObject), mapping it to the table of your choice, and define a one-to-many relationship between MyObject and ObjectB. saveOrUpdate saves passed object and all its fields, so the array of ObjectB will be saved too.

As for ObjectA or ObjectC you can either implement a UserType or mark them as @Embedded (if you want their fields in the same table the MyObject fields are), or map them as entities to another table (as in ObjectB case).

2 Comments

Since they don't belong in the same table I'd have to use UserType, but then the good looking hibernate code would start looking bad in my opinion and I might as well use Spring/JDBC.
If they don't belong in the same table do not trouble yourself with writing UserType, just map all the objects as entities to different tables, specify relationships, and everything should work fine.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.