Hibernate insert data in 2 tables
I have a channel table with channel_id PK. and one ad_config table which will have channel_id and serverSrc as composite key. relation is for each channel_id there will be 2 rows inserted in server_config for 开发者_StackOverflowe.g. Channel table: channel_id : 100, channelName:xyz.....(other channel data) Ad_config 1 : channel_id: 100, serverSrc:0 Ad_config 2 : channel_id: 100, serverScr:1
How can I do this using Hibernate? Currently I only have a channel object. I can create other objects if required, but I don't have any idea as what objects to create, what should be the hibernate mapping and how shud be the call in DAO class.
You should have two objects: Channel and AdConfig. A Channel has a List (maybe an indexed collection). You'll map them as @OneToMany in one side and @ManyToOne in the other side. Refer to the Hibernate documentation for details and examples:
http://docs.jboss.org/hibernate/core/3.5/reference/en/html/collections.html#collections-indexed
Also, take a look at the test suite. There are plenty of usage examples there. For instance:
https://github.com/hibernate/hibernate-core/tree/master/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement
精彩评论