Hibernate _ note 22 (data in XML)

Source: Internet
Author: User

It indicates that the data XML in XML is only a text file format. It does not have the ability to inherit data storage or data management media. The XML data model is fragile. Its type system is complex and low-energy. Its data integrity is almost completely procedural, and it also introduces a layered data structure that was eliminated decades ago. However, data in XML format is very attractive for Java, because we have some good tools. For example, you can use XSLT to convert XML data, which is one of the best cases we think.
Hibernate does not have built-in functions to store data in XML format. It depends on Relational representation and SQL. The benefits of this policy should be clear. On the other hand, Hibernate can load and present data to developers of applications in XML format. It allows you to use a complex set of tools without any extra conversion steps.
Suppose you work in the default POJO mode and want to get some data in XML format soon. Use EntityMode. DOM4J to open a temporary Session:

Session dom4jSession = session.getSession(EntityMode.DOM4J);Element userXML = (Element) dom4jSession.get(UserPojo.class,new Long(1));
Here, a dom4j Element is returned. You can use this dom4j API to read and operate it. For example, you can use the following snippet to print it beautifully to the console:
try {    OutputFormat format = OutputFormat.createPrettyPrint();    XMLWriter writer = new XMLWriter(System.out, format);    writer.write(userXML);} catch (UnsupportedEncodingException e) {    e.printStackTrace();} catch (IOException e) {    e.printStackTrace();}
Suppose you reuse the POJO class and data from the previous examples, you will see a UserPojo instance and two ItemPojo instances.
     
  
   1
      
  
   zhangsan
      
          
               
    
     2
                
    
     99
                
    
     An item for auction
                
    
     1
            
           
               
    
     3
                
    
     1000
                
    
     Another item for auction
                
    
     1
            
       
  
 
Note: At this time, if you run, the JVM overflow exception should be reported. Solution
1) set embed-xml = "true" (whether to include xml, Yes)
2) Set embed-xml = "false" (whether to include xml or No) for multiple parties)
You can add the node attribute to the Hibernate ing metadata to change the default XML Representation:
 
  
   
    
     SEQ_ITEM_ENTITY_ID
    
   
   
   
   
  
  
   
    
     SEQ_USER_ENTITY_ID
    
   
   
   
    
    
   
  
 
Each node attribute defines an XML Representation:
1) The node = "name" attribute in the ing defines the name of the XML element for the object.
2) For the node = "name" attribute in any property ing, the specified property content should represent the XML element text of the given name.
3) for the node = "@ name" attribute in any property ing, the specified property content should represent the XML property value of the given name.
4) for the node = "name/@ attname" attribute in any property ing, the specified property content should represent the XML property value of the given name in a child element of the given name.
Updated mappings generate the following XML Representation of the same data you have seen before:
    
       
          
         
        
          
         
      
   
  
Finally, the data in XML format is transaction and persistent. Therefore, you can modify the XML element to be queried and let Hibernate update the underlying table:
Element itemXML = (Element) dom4jSession. get (ItemPojo. class, new Long (2); itemXML. element ("item-details "). attribute ("inner-price "). setValue ("100000"); dom4jSession. flush (); // Hibernate executes UPDATES can be understood as it should have been delayed until the transaction was committed for modification, and now the modification of Element userXML = (Element) dom4jSession is immediately executed. get (UserPojo. class, new Long (1); Element newItem = incluenthelper. createElement ("item"); Element newItemDetails = newItem. addElement ("item-details"); newItem. addAttribute ("seller-id", userXML. attribute ("id "). getValue (); // set the correlation // Add item details newItemDetails. addattriils ("inner-price", "9999"); newItemDetails. addAttribute ("description", "description"); dom4jSession. save (Item. class. getName (), newItem); dom4jSession. flush ();
Finally, if you like annotations, you can use all three built-in entity modes at the same time. You can map a static POJO Implementation of the domain model, switch to a dynamic ing for the general user interface, and export data to XML. Alternatively, you can write an application that does not have any domain classes, only dynamic ing and XML. However, we must remind you that prototype in the software industry often means that customers end up with a prototype that nobody wants to discard-are you willing to buy a model car? We strongly recommend that you rely on static Domain Models if you want to create a maintenance system.
Dynamic model or XML Representation will not be considered later. The focus is on static persistence classes and their ing methods.
To sum up, I feel like I have learned a little more about the concept, but I learned it by myself. The progress may be a little slow.
This chapter focuses on the cross-cutting concerns of domain model separation, followed by the entity programming model (POJO, JPA entity programming model), with the model, of course, the ing method is selected (1. XML ing file 2, JDK 5.0 annotation 3, XDoclet), and metadata like XML ing can be globally defined, the use of XML point operators... It can be said that there are many things, and we hope you can master them well.

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.