Hi there!
I am a new Hibernate- User and have a problem. I generated my java classes from a xsd- file and uses JPA to fill my POJO's with MySQL- data. I came up to now relatively well with Hibernate and JAXB since now. At first I will show you the relevant parts of my xsd file, then I'll show the relevant classes and I hope someone can tell me how to annotate it.
XSD (Schema):Code:
<xsd:element name="record" type="complexType.InventoryRecord"/>
<xsd:complexType name="complexType.InventoryRecord">
<xsd:sequence>
<xsd:element name="allocation" type="simpleType.Allocation" minOccurs="0"/>
<xsd:element name="allocation-timestamp" type="xsd:dateTime" minOccurs="0"/>
<xsd:element name="perpetual" type="xsd:boolean" minOccurs="0"/>
<xsd:element name="preorder-backorder-handling" type="simpleType.InventoryRecord.PreorderBackorderHandling" minOccurs="0"/>
<xsd:element name="preorder-backorder-allocation" type="simpleType.Allocation" minOccurs="0"/>
<xsd:element name="in-stock-date" type="xsd:date" minOccurs="0"/>
<xsd:element name="custom-attributes" type="sharedType.CustomAttributes" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="product-id" type="simpleType.Generic.NonEmptyString.256" use="required"/>
<xsd:attribute name="mode" type="simpleType.ImportMode" use="optional"/>
</xsd:complexType>
<xsd:complexType name="sharedType.CustomAttributes" mixed="false">
<xsd:sequence>
<xsd:element name="custom-attribute" type="sharedType.CustomAttribute" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="sharedType.CustomAttribute" mixed="true">
<xsd:sequence>
<xsd:element name="value" type="simpleType.Generic.String" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="attribute-id" type="simpleType.Generic.NonEmptyString.256" use="required"/>
<xsd:attribute ref="xml:lang"/>
</xsd:complexType>
I generate the XML without the CustomAttributes- part, because there is my problem... I dont get it to work to create the CustomAttributes- Part in my XML.
Here is a sample- XML that should be generated:
Code:
<records>
<record mode="delete" product-id="!">
<allocation>0.0</allocation>
<allocation-timestamp>2001-12-17T09:30:47Z</allocation-timestamp>
<perpetual>true</perpetual>
<preorder-backorder-handling>none</preorder-backorder-handling>
<preorder-backorder-allocation>0.0</preorder-backorder-allocation>
<in-stock-date>1967-08-13</in-stock-date>
<custom-attributes>
<custom-attribute xml:lang="en-us" attribute-id="!">
<value>String</value>
<value>String</value>
<value>String</value>
</custom-attribute>
<custom-attribute xml:lang="en-us" attribute-id="!">
<value>String</value>
<value>String</value>
<value>String</value>
</custom-attribute>
</custom-attributes>
</record>
</records>
The Generated Java- classes are:Code:
// This is a container class for records
@Entity
@Table(name="dw_exp_inventory")
public class RecordCartrige {
@Id
@GeneratedValue
@Column(name="rec_id")
private long recId;
@Embedded
private ComplexTypeInventoryRecord record;
...
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "complexType.InventoryRecord", propOrder = {
"allocation",
"perpetual",
"preorderBackorderHandling",
"preorderBackorderAllocation",
"inStockDate"
})
@Embeddable
public class ComplexTypeInventoryRecord {
@XmlElement
@Column
protected BigDecimal allocation;
@XmlElement
@Column
protected Boolean perpetual;
@XmlElement(name="preorder-backorder-handling")
@Column(name="preorder_backorder_handling")
protected SimpleTypeInventoryRecordPreorderBackorderHandling preorderBackorderHandling;
@XmlElement(name="preorder-backorder-allocation")
@Column(name="preorder_backorder_allocation")
protected BigDecimal preorderBackorderAllocation;
@XmlElement(name="in-stock-date")
@XmlSchemaType(name="date")
@XmlJavaTypeAdapter(type=String.class, value=DateAdapter.class)
@Column(name="in_stock_date")
protected Date inStockDate;
@XmlAttribute(name="product-id", required=true)
@Column(name="product_id", nullable=false)
protected String productId;
@XmlAttribute
@Column(nullable=false)
protected String mode;
// How I have to do this???
@XmlElement(name = "custom-attributes")
protected SharedTypeCustomAttributes customAttributes;
...
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sharedType.CustomAttributes", propOrder = {
"customAttribute"
})
public class SharedTypeCustomAttributes {
protected List<SharedTypeCustomAttribute> customAttribute;
public List<SharedTypeCustomAttribute> getCustomAttribute() {
if (customAttribute == null) {
customAttribute = new ArrayList<SharedTypeCustomAttribute>();
}
return this.customAttribute;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sharedType.CustomAttribute", propOrder = {
"content"
})
public class SharedTypeCustomAttribute {
@XmlElementRef(name="value", namespace = "http://www.demandware.com/xml/impex/inventory/2007-05-31", type = JAXBElement.class)
@XmlMixed
protected List<Serializable> content;
@XmlAttribute(name="attribute-id", required = true)
protected String attributeId;
@XmlAttribute(namespace="http://www.w3.org/XML/1998/namespace")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name="language")
protected String lang;
...
}
I get the RecordCartrige- Objects with the following code:
Code:
List<RecordCartrige> recordList = (List<RecordCartrige>) getSession().createQuery("from " + RecordCartrige.class.getName()).list();
Iterator<RecordCartrige> iterator = recordList.iterator();
and later I can iterate through the List to write my XML:
Code:
while(iterator.hasNext()) {
RecordCartrige recordCartrige = iterator.next();
JAXBElement<ComplexTypeInventoryRecord> record = jaxbObjFactory.createRecord(recordCartrige.getRecord());
getMarshaller().marshal(record, getWriter());
}
In Class "ComplexTypeInventoryRecord" there is the field customAttributes which is from type "SharedTypeCustomAttributes". What I have to do now? SharedTypeCustomAttributes has a Collection/List of SharedTypeCustomAttribute- Objects and is only a "container" for SharedTypeCustomAttribute. For Example: Must I add the Entity- Annotation to the SharedTypeCustomAttributes- Class? The values for one CustomAttribute are in a different table as the record- values (they have their own DB-table) It would be nice if someone could help me...
Thanks!
Danny