Serializable session.begin Transaction(); Stock stock = new Stock(); Stock Code("7052"); Stock Name("PADINI"); Category category1 = new Category("CONSUMER", "CONSUMER COMPANY"); //new category, need save to get the id first session.save(category1); Stock Category stock Category = new Stock Category(); stock Stock(stock); stock Category(category1); stock Created Date(new Date()); //extra column stock Created By("system"); //extra column Stock Categories().add(stock Category); session.save(stock); Transaction().commit(); Hibernate: insert into mkyongdb.category (`DESC`, NAME) values (? ) Hibernate: insert into mkyongdb.stock (STOCK_CODE, STOCK_NAME) values (?
Table; @Entity @Table(name = "category", catalog = "mkyongdb") public class Category implements
In this tutorial, it will reuse the entire infrastructure of the previous “Hibernate one to many relationship example – XML mapping” tutorial, enhance it to support Hibernate / JPA annotation.
Unique Constraint; @Entity @Table(name = "stock", catalog = "mkyongdb", unique Constraints = ) public class Stock implements
You need to design bidirectional relationships carefully, considering use cases (do you need navigational access in both directions? See also: Hi,thanks,seems that you are a experter of hibernate,since you have answer my question :stackoverflow.com/questions/5350770/… And now I am not sure the relationship holding,since I can not write more in the comment,so I post it here dpaste.de/J85m .
Please have a check if possible.:) In terms of coding, a bidirectional relationship is more complex to implement because the application is responsible for keeping both sides in synch according to JPA specification 5 (on page 42).
In order to manage the size of the post, i have skipped the textual descriptions of some basic stuff.
In case you are interested in those details, this ,this & this post will help you. Unique Constraint; @Entity @Table(name = "stock_daily_record", catalog = "mkyongdb", unique Constraints = @Unique Constraint(column Names = "DATE")) public class Stock Daily Record implements Hibernate Util; public class App Hibernate one to many (Annotation) Hibernate: insert into mkyongdb.stock (STOCK_CODE, STOCK_NAME) values (? ) Hibernate: insert into mkyongdb.stock_daily_record (DATE, PRICE_CHANGE, PRICE_CLOSE, PRICE_OPEN, STOCK_ID, VOLUME) values (? This is a Unidirectional association The main differenece is that bidirectional relationship provides navigational access in both directions, so that you can access the other side without explicit queries.Also it allows you to apply cascading options to both directions. This post demonstrates a complete application with complete code.