Definition of XML Mapping
XML Mapping is defined as an editor used to map one or more XML Document to a common XML file by this, we can generate a transformation document after defining Mapping. Mapping does delete, editing or any persistent work. The Relationship of Objects is normally defined in an XML furthermore thus XML file instructs the hibernate to show how a class are a map to the Table. The mapping document is saved with the format classname.hbm.xml i.e hibernate Mapping file.
Syntax:
Class for Mapping an element using Hibernate.
<class name="Event" table="EVENTS">
...
</class>
In a Hibernate way it looks like:
<persistence-unit name="externalMapping" transaction-type="Reserved">
<provider>org </provider>
<mapping-file>file:///path// orm</mapping-file>
<properties>
<property name="xxx" value=""/>
</property name>
In the next section, let’s take a more detailed look at the Performance of XML mapping. To start with, we can either define them with annotations or in XML file.
How to perform Mapping in XML?
To perform a mapping, we need three types of files namely
- An XML File
- XML Schema
- Document Type Definition
Here we have used Hibernate a java Program for data manipulation and access also performs Object – relationship mapping tool for java Program. In other case, XML schema annotations fall with Mapping to determine the Tables. In case of XSD mapping of files is performed from Source XML schema to Target XML.
To perform Mapping on XML representations we have defined a class as follows. The following code displays the sample annotations on Mapping. By default Hibernate load the orm.xml file.
@XmlRootElement(name="client", namespace="http://educba.org/demo")
public class client {
private String cname;
@XmlElement(namespace="http://educba.org/demo")
public String getName() {
return cname;}
public void setName(String cname) {
this.cname = cname;
}
}
Next is performing XML Object
<?xml version="1.0" encoding="UTF-8"?>
<client xmlns="http://educba.org/test">
<cname>Kennelly van</cname>
</client>
import static org.educba.spin.Spin.XML;
String inxml = "<?xml version="1.0" encoding="UTF-8"?>
<client xmlns="http://educba.org/test">
<cname>Kennelly van</cname>
</client>";
Client ce = XML(inxml).mapTo(Client.class);
When a mapping property is updated the complete XML, the file is Parsed and the necessary property is applied to an element. A Valid XML file Mapping requires a list of Properties, namespaces, and Schema identifier. Next, a global parameter should be added to XML file.
Types
Let us discuss types of XML Mapping.
1. Direct Mapping
This Mapping performs a mapping with a Java attribute to XML text nodes. Direct Mapping is performed in the following case:
Text node, attribute, List-Field, and Union.
2. XML Composite
This mapping performs a collection of files to XML text Nodes.
3.XML Transformation Mapping
This performs custom mapping where the XML node creates the Object to be stored in java class file.
4. XML Object Mapping
Mapping single attribute to elements in the XML Document.
In this article, we shall see how to use XML to map one-to-many associations using a framework Hibernate with java Objects and a database Table.
Step-1: The First Step is to create a Database in Mysql to create a Table.
Step-2: Hibernate class
Step-3: Mapping two XML files
Note: To retrieve data from a tree structure of XML File we need to specify a map between the tables using SAS XML Map. It includes drag and drops facilities.
Example
Example #1
Consider an Objects need to Store and retrieve using below RDBMS table.
Using SQL to create a database
CREATE TABLE Airline(AId integer PRIMARY KEY, AName text);
INSERT INTO Airline VALUES(401,'Emirates');
INSERT INTO Airline VALUES(202,'Lufhansa');
INSERT INTO Airline VALUES(314,'Jet Airways');
INSERT INTO Airline VALUES(465,'Spice');
INSERT INTO Airline VALUES(545,'Malaysian Airlines');
COMMIT;
SELECT * FROM Airline;
Airline.java
package net.codejava.hibernate;
import java.util.Set;
public class Airline {
private long Aid;
private String Aname;
private Set<Fuels> fuel;
public Airline() {
}
public Airline(String Aname) {
this.Aname = Aname;
}
}
Manufacturing.java
package net.educba.hibernate;
public class Manufacturing {
private long Mid;
private String Mname;
private String Desc;
private float Amount;
private Airline airline;
public Manufacturing() {
}
public Manufacturing(String Mname, String Desc, float Amount,
Airline airline) {
this.Mname = Mname;
this.Desc = Desc;
this.Amount = Amount;
this.Airline = Airline;
}
}
Creating Two XML files for Mapping the Files
Based on the entities defined a map file is created which intimates the class and database tables. Making two hibernate Mappin Files. So here we have a one-to-many association.
Airline.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="net.educba.hibernate">
<class name="Airline" table="Airline">
<id name="Aid" column="Section_ID">
<generator class="native"/>
</id>
<property name="Aname" column="ANAME" />
<set name="manufacturing" inverse="true" cascade="all">
<key column="Section_ID" not-null="true" />
<one-to-many class="Manufacturing"/>
</set>
</class>
</hibernate-mapping>
Manufacturing.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="net.educba.hibernate">
<class name="Manufacturing" table="MANUFACTURING">
<id name="Mid" column="M_ID">
<generator class="native"/>
</id>
<property name="Mname" column="MNAME" />
<property name="desc" column="DESC" />
<property name="amt" column="Amt" type="float" />
<many-to-one name="Airline" class="Airline"
column="M_ID" not-null="true"/>
</class>
</hibernate-mapping>
Test coding
package net.EDUCBA.hibernate;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class AirlineManager {
public static void main(String[] args) {
Configuration cf = new Configuration().cf();
ServiceRegistryBuilder reg = new ServiceRegistryBuilder();
reg.applySettings(cf.getProperties());
ServiceRegistry sr = reg.buildServiceRegistry();
SessionFactory sf = configuration.buildSessionFactory(sr);
Session sess = sf.openSession();
sess.beginTransaction();
Airline airline = new airline("");
Manufacturing engine = new Product("ABX", "Partial core PC", 1000, Airline);
Manufacturing Elevator = new Product("XXX", "High-end", 5000,Airline);
Manufacturing Alirones = new Product("YYY", "Low-end", 465,Airline);
Manufacturing Tass = new Product("XYXZ", "High High End", 1578, Airline);
Set<Manufacturing> Manufacturings = new HashSet<Manufacturing>();
Manufacturings.add(engine);
Manufacturings.add( Elevator);
Manufacturings.add( Alirones);
Manufacturings.add(Tass);
Airline.setProducts(id);
sess.save(Airline);
sess.getTransaction().commit();
sess.close();
}
}
Explanation
As shown in the above example, we have to include XML Mapping into a class with No-restrictions. Here a single Airline may contain one or more Manufacturing. When we execute it hibernate will insert a row in the Database Table.
Output:
Conclusion
Coming to an end, the XML file Mapping in Hibernate allows to store files on mapping anywhere provided Mapping file URL is accessible. XML Mapper is a useful tool to read XML files using SAS and it is very easy to use. This is gaining value as it has a role of exchanging a wide variety of data with drag and drop features. With the increased demand for technical advancements, it is good to learn new optimal Solutions.
Recommended Articles
This is a guide to XML Mapping. Here we discuss the How to perform mapping in XML? with examples and its code implementation. You may also have a look at the following articles to learn more –