Inverse, cascade, lazy, unsaved-Value

Source: Internet
Author: User

 

This example is constructed to illustrate some advanced configurations of Hibernate and their related mechanisms during the test:
There are three classes: category. Java, prodcuct. Java, and configurationtest. java. The third class is used for testing.
Category. Java code:
  1. Package unsaved_value;
  2. Import ......
  3. Public class category {
  4. Private integer ID;
  5. Private string name;
  6. Private string description;
  7. Private set Products;
  8. Public category (){
  9. Id = NULL;
  10. Name = NULL;
  11. Description = NULL;
  12. Products = new hashset ();
  13. }
  14. Public void addproduct (product P ){
  15. Products. Add (P );
  16. }
  17. // ********** Setter and getter
  18. ........
  19. }
 
Product. Java code:
 
  1. Package unsaved_value;
  2. Public class product {
  3. Private integer ID;
  4. Private string name;
  5. Private category;
  6. Private string description;
  7. Public Product (){
  8. }
  9. // ******* Getter and setter
  10. .........
  11. }
Configurationtest. Java
  1. Public void testsave () throws exception {
  2. CATEGORY Category = new category ();
  3. Category. setname ("Java programming book 2 ");
  4. Category. setdescription ("programming classic books 2 ");
  5. Product pro = new product ();
  6. Pro. setname ("Java programming ideology 2 ");
  7. Pro. setdescription ("Chinese Version 4 2 ");
  8. Pro. setcategory (category );
  9. Category. addproduct (Pro );
  10. Transaction Tx = session. begintransaction ();
  11. Assert (session! = NULL) :( "session is null ");
  12. Session. Save (category );
  13. TX. Commit ();
  14. }

Category represents the product catalog, while product represents the product. Obviously, category and product are one-to-multiple relationships. There are two ways for hibernate to map one-to-multiple relationships. One is one-to-one, and the other is two-way. Compared with the two, the two-way one-to-multiple advantage is reflected in two aspects: first, it is also very obvious that because of the two-way association, we will be more convenient in actual business logic, for example, we can retrieve all the products under a category and find out which product belongs. Second, bidirectional relationships are more advantageous in accessing databases than unidirectional ones. This will be discussed later in inverse.
. Two-way Association is the only "disadvantage" of one-way Association, because two-way association needs to write more ing files than one-way Association. This is not a problem. Two-way Association is used to map these two databases:
  1. Category. HBM. xml:
  2. Version = "1.0" encoding = "UTF-8"?>
  3. Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
  4. <Hibernate-mapping package = "unsaved_value">
  5. <Class name = "category" table = "category">
  6. <ID name = "ID" column = "ID">
  7. <Generator class = "native">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.