How to implement One-to-Many mapping when the associative table is one for all one-many relationships?

Suggest you create multiple views in the database filtered on the association_name column, and then define many-to-many relationships in your mapping based on these, and just enforce the one-to-many semantics in your code. This situation with an intermediate table is only really handled in bernate using a many-to-many mapping.

This is normally called a qualified association. Such association should be realized with Map instead of List. There is not built in support for these in hibernate.

If you don't care about the qualifier, the association become a plain many-to-many that can be handled with hibernate. But in your case this isn't a qualified association at the logical level. If I understand well, one association table is used to represent what is actually several associations: If you can change the db schema to have on table per logical association, the problem vanishes.

Using database views to "simulate" having separate tables (as suggested by David M) is an alternative. Create one view per logical association and map it as a many-to-many.

Another solution that work maybe would be to have a polymorphic entity for NodeAssociation in hibernate. There would be one NodeAssociationX entity per type of association. There is then a one-to-many association between Project and NodeAssociation.

And there is a one-to-many per NodeAssociationX and the corresponding table/entity (Category, etc. ). You can map the private fields so that you can provide public getter/setter that provide the correct view at the logical level. Public List getCategories() { List categories = new ArrayList(); for( NodeAssociation na : nodeAssociations ) { if( typeof( na ) = NodeAssociationCategory ) { categories.

AddAll( ((NodeAssociationCategory)na).getCategories() ); } } return categories; } But this is rather ugly. Try the other solution if possible.

Entity public class Category { private Integer id; @Id public Integer getId() { return this. Id; } private LinkBetweenOneCategoryAndManyProject linkClass = new LinkBetweenOneCategoryAndManyProject(); @OneToOne public LinkBetweenOneCategoryAndManyProject getLinkClass() { return this. LinkClass; } public void addProject(Project project) { getLinkClass().getProjectList().

Add(project); } // delegates to LinkClass public String getAdditionalProperty() { getLinkClass(). GetAdditionalProperty(); } } Now our Project @Entity public class Project { private Integer id; @Id public Integer getId() { return this. Id; } } Now our LinkBetweenOneCategoryAndManyProject class @Entity public class LinkBetweenOneCategoryAndManyProject { private Integer categoryId; private String additionalField; List projectList; @Id public Integer getCategoryId() { return this.

CategoryId; } @OneToMany public List getProjectList() { return projectList; } public String additionalProperty() { return this. AdditionalField; } } So if you want to retrieve a Category and its project, do as follows select distinct c from Category c left join fetch c. LinkedClass lc left join fetch lc.

ProjectList Takes care I suppose the relationship between Category and LinkBetweenOneCategoryAndManyProject is OneToOne regards.

I found a suitable solution with native queries. In a nutshell: I made simple mapping for Project and Category without any relationships (like OneToMany) I add field category to Project entity and marked it as @Transient In ProjectDAO I manually insert category to project, using native queries. Article about JPA Native Queries and related jboss docs were a startpoint for me.

Then you don't have automatic persistence of the relationship by hibernate:if you add a category to your project, and save the project, you will need to persist the category and the association yourself. You lose a lot of the power of hibernate then. The DAO could do the job if you save object using it, but the you can't simply load/modify entities, you need to load/modify/save each time.

Maybe an EntityListener could do the job in a more transparent way. – ewernli Jan 5 '10 at 14:06 I'll read about EntityListener. You're right and I knew all this before your comment appeared but I don't see any other way out.

I can't modify DB schema. My application only reads from DB and this allows me to change objects manually. Otherwise I would probably think twice before applying my solution.

– Roman Jan 5 '10 at 14:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions