Last active
May 28, 2017 09:52
-
-
Save cjmamo/4052116 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
@Test | |
public void testMapping() throws Exception { | |
// Hibernate configuration | |
Map<String, String> hibernateProperties = new HashMap<String, String>(); | |
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.DerbyTenSevenDialect"); | |
hibernateProperties.put("hibernate.connection.driver_class", "org.apache.derby.jdbc.EmbeddedDriver"); | |
hibernateProperties.put("hibernate.connection.url", "jdbc:derby:target/test-database/database;create=true"); | |
hibernateProperties.put("hibernate.hbm2ddl.auto", "create"); | |
// initialise Hibernate | |
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.opensourcesoftwareandme", hibernateProperties); | |
EntityManager em = emf.createEntityManager(); | |
// deserialize test XML document | |
JAXBElement jaxbElement = (JAXBElement) JAXBContextUtils.unmarshal("org.opensourcesoftwareandme", readFileAsString("src/test/resources/mails.xml")); | |
MailsType mails = (MailsType) JAXBElementUtils.getValue(jaxbElement); | |
// persist object | |
em.getTransaction().begin(); | |
em.persist(mails); | |
em.getTransaction().commit(); | |
// retrieve persisted object | |
MailsType persistedMails = (MailsType) em.createQuery("from MailsType where hjid = 1").getSingleResult(); | |
assertEquals("[email protected]", persistedMails.getMail().get(0).getEnvelope().getFromEnvelope()); | |
assertEquals("[email protected]", persistedMails.getMail().get(0).getEnvelope().getTo()); | |
em.close(); | |
emf.close(); | |
} | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" | |
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" | |
xmlns:xs="http://www.w3.org/2001/XMLSchema" | |
version="2.1"> | |
<bindings schemaLocation="mails.xsd" version="1.0"> | |
<!-- Generate POJOs that comply with JPA spec --> | |
<globalBindings localScoping="toplevel"> | |
<serializable/> | |
</globalBindings> | |
<!-- Customise the package name --> | |
<schemaBindings> | |
<package name="org.opensourcesoftwareandme"/> | |
</schemaBindings> | |
<!-- rename the From getter & setter to avoid a came conflict --> | |
<bindings node="//xs:complexType[@name='envelopeType']"> | |
<bindings node=".//xs:attribute[@name='From']"> | |
<property name="FromEnvelope"/> | |
</bindings> | |
</bindings> | |
</bindings> | |
</bindings> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> | |
<xsd:element name="mails" type="mailsType"/> | |
<xsd:complexType name="mailsType"> | |
<xsd:sequence minOccurs="0" maxOccurs="unbounded"> | |
<xsd:element name="mail" type="mailType"/> | |
</xsd:sequence> | |
</xsd:complexType> | |
<xsd:complexType name="mailType"> | |
<xsd:sequence> | |
<xsd:element name="envelope" type="envelopeType"/> | |
<xsd:element name="body" type="bodyType"/> | |
<xsd:element name="attachment" type="attachmentType" | |
minOccurs="0" maxOccurs="unbounded"/> | |
</xsd:sequence> | |
<xsd:attribute use="required" name="id" type="xsd:integer"/> | |
</xsd:complexType> | |
<xsd:element name="header"> | |
<xsd:complexType> | |
<xsd:simpleContent> | |
<xsd:extension base="xsd:string"> | |
<xsd:attribute ref="name" use="required"/> | |
</xsd:extension> | |
</xsd:simpleContent> | |
</xsd:complexType> | |
</xsd:element> | |
<xsd:element name="Date" type="xsd:dateTime"/> | |
<xsd:complexType name="envelopeType"> | |
<xsd:sequence> | |
<xsd:element name="From" type="xsd:string"/> | |
<xsd:element name="To" type="xsd:string"/> | |
<xsd:element ref="Date"/> | |
<xsd:element name="Subject" type="xsd:string"/> | |
<xsd:element ref="header" minOccurs="0" maxOccurs="unbounded"/> | |
</xsd:sequence> | |
<xsd:attribute name="From" type="xsd:string" use="required"/> | |
</xsd:complexType> | |
<xsd:simpleType name="bodyType"> | |
<xsd:restriction base="xsd:string"/> | |
</xsd:simpleType> | |
<xsd:complexType name="attachmentType"> | |
<xsd:group ref="attachmentContent"/> | |
<xsd:attribute ref="name" use="required"/> | |
</xsd:complexType> | |
<xsd:group name="attachmentContent"> | |
<xsd:sequence> | |
<xsd:element name="mimetype"> | |
<xsd:complexType> | |
<xsd:attributeGroup ref="mimeTypeAttributes"/> | |
</xsd:complexType> | |
</xsd:element> | |
<xsd:element name="content" type="xsd:string" minOccurs="0"/> | |
</xsd:sequence> | |
</xsd:group> | |
<xsd:attribute name="name" type="xsd:string"/> | |
<xsd:attributeGroup name="mimeTypeAttributes"> | |
<xsd:attribute name="type" type="mimeTopLevelType" use="required"/> | |
<xsd:attribute name="subtype" type="xsd:string" use="required"/> | |
</xsd:attributeGroup> | |
<xsd:simpleType name="mimeTopLevelType"> | |
<xsd:restriction base="xsd:string"> | |
<xsd:enumeration value="text"/> | |
<xsd:enumeration value="multipart"/> | |
<xsd:enumeration value="application"/> | |
<xsd:enumeration value="message"/> | |
<xsd:enumeration value="image"/> | |
<xsd:enumeration value="audio"/> | |
<xsd:enumeration value="video"/> | |
</xsd:restriction> | |
</xsd:simpleType> | |
</xsd:schema> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.jvnet.hyperjaxb3</groupId> | |
<artifactId>maven-hyperjaxb3-plugin</artifactId> | |
<executions> | |
<execution> | |
<goals> | |
<goal>generate</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<dependencies> | |
<dependency> | |
<groupId>org.jvnet.hyperjaxb3</groupId> | |
<artifactId>hyperjaxb3-ejb-runtime</artifactId> | |
<version>0.5.6</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-entitymanager</artifactId> | |
<version>4.1.7.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.derby</groupId> | |
<artifactId>derby</artifactId> | |
<version>10.9.1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.9</version> | |
</dependency> | |
</dependencies> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment