Anatoly
Anatoly

Reputation: 11

Hibrernate: Attribute "type" must be declared for element type "column"

I am trying to save to two tables in SQL server using Hibernate: ORDER and ORDER_ITEM I get an error: Attribute "type" must be declared for element type "column". Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML.

This produces a NullPointerException

If I understand correctly, this means that when I try to save to the order_item table, the getter for the foreign key is empty, but how would I set it if is designed to 'Autoincrement', I thought that hibernate would handle this in it's transaction.

Below are my POJO's and Hibernate mappings. I have omitted the getters and setters from this copy/paste, but they are present in my actual code

I am also successful in saving just to the ORDER table if I remove the set

Order.java:

public class Order {
public Order(){
    super();       
}

private int orderId;        
private Set<LineItem> items;         
private String strPhone;          
private String strEmail;    
private String strFirstName;   
private String strLastName;  
private String strParentFirstName;    
private String strParentLastName;    
private String strOrganizationName;    
private String strOrganizationType;         
private String strComment;         

}

order.hbm.xml:

<hibernate-mapping>
<class name="dbobjects.Order" table="orders">     

    <id name="orderId" type="integer" column="order_id">
        <generator class="increment"/>
    </id>

    <property name="strPhone"            type="string" column="phone_number"/>
    <property name="strFirstName"        type="string" column="first_name"/>
    <property name="strLastName"         type="string" column="last_name"/>
    <property name="strParentFirstName"  type="string" column="parent_first_name"/>
    <property name="strParentLastName"   type="string" column="parent_last_name"/>
    <property name="strOrganizationName" type="string" column="organization_name"/>
    <property name="strOrganizationType" type="string" column="organization_type"/>        
    <property name="strComment"          type="string" column="comments"/>

    <set name="items" table="order_item" fetch="select" cascade="all">
        <key>
            <column name="orderId" type="java.lang.Integer"/>                
        </key>
        <one-to-many class="dbobjects.LineItem"/>
    </set>

</class>    

LineItem.java:

public class LineItem {

public LineItem(){
    super();
}

private int orderItemId;//this will be the primary key
private int orderId;//this is the foreign key to the order 
private String age;    
private String gender;    
private String type;
private String itemSize;        
private int itemQuantity;

}

lineItem.hbm.xml:

<id column="order_item_id" name="orderItemId" type="integer">
  <generator class="increment"/>
</id>

<property column="age" name="age" type="string"/>
<property column="gender" name="gender" type="string"/>
<property column="quantity" name="itemQuantity" type="integer"/>
<property column="size" name="itemSize" type="string"/>
<property column="clothing_type" name="clothingType" type="string"/>

<many-to-one name="orderId" class="dbobjects.Order" fetch="select" column="order_id" type="java.lang.Integer"/>

This is where the error is thrown when I Instanciate the session:

**session = HibernateUtil.getSessionFactory().openSession();**←
        try{        
        session.beginTransaction();
        session.save(order);
        session.getTransaction().commit();

So the question is: How do I handle this situation with 'autoincrement' primary key that is not accessible to another table as a foreign key

Upvotes: 0

Views: 2798

Answers (3)

Anatoly
Anatoly

Reputation: 11

So, There were a few problems:

In order to submit a 'child' to the database, I needed to show who is the parent. This means that my LineItem Class needed a

Order order;

property instead of a simple

private String orderID;

this was accomplished after the Order object was ready to be submitted to the DB but before the actual call:

 for (LineItem item : order.getItems()) {
            item.setOrder(order);
        }

I also changed my DTD's from !DOCTYPE hibernate-configuration SYSTEM classpath://org/hibernate/hibernate-mapping-3.0.dtd"

to

!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"

Upvotes: 1

Karbos 538
Karbos 538

Reputation: 3055

Try to replace :

<many-to-one name="orderId" column="order_id" class="dbobjects.Order" cascade="all">
    <column name="orderId" type="java.lang.Integer"/>   
</many-to-one>

by

<many-to-one name="orderId" column="order_id" class="dbobjects.Order" cascade="all"/>

Upvotes: 0

Karbos 538
Karbos 538

Reputation: 3055

Just add "type" attribute like this :

<column name="orderId" type="java.lang.Integer"/>

Upvotes: 0

Related Questions