This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes problem saving with foreign key error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "problem saving with foreign key error" Watch "problem saving with foreign key error" New topic
Author

problem saving with foreign key error

Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator

Hi all,
i am having some issues in saving with tables with foreign key relationship. i got an error failed generate key
i got one table
ras_order
order_id number(2) primary key
order_name varchar2(100)

another table by name
ras_order_detail
order_item_id number(2) primary key,
order_item_name varchar2(100),
order_id references ras_order(order_id)

here is my basic java bean files
Order.java
public class Order {
private int order_id =0;
private String order_name = null;
private Set orderItems;

public void setOrderItemsL(Set orderitems) {
this.orderItems = orderitems;
}

public Set getOrderItemsL() {
if (this.orderItems == null) {
this.orderItems = new HashSet();
}
return this.orderItems;
}


public int getOrder_id() {
return order_id;
}

public void setOrder_id(int order_id) {
this.order_id = order_id;
}

public String getOrder_name() {
return order_name;
}

public void setOrder_name(String order_name) {
this.order_name = order_name;
}

}

OrderItem.java
public class OrderItem {
private int order_item_id = 0;
private String order_item_name = null;
private Order order = null;

public Order getOrder() {
return order;
}

public void setOrder(Order order) {
this.order = order;
}

public int getOrder_item_id() {
return order_item_id;
}

public void setOrder_item_id(int order_item_id) {
this.order_item_id = order_item_id;
}

public String getOrder_item_name() {
return order_item_name;
}

public void setOrder_item_name(String order_item_name) {
this.order_item_name = order_item_name;
}
}

here is my java code
String str = "index";
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
Order order = new Order();
order.setOrder_name("TEST ORDER");
OrderItem oid = new OrderItem();
oid.setOrder_item_name("some name");
oid.setOrder(order);
Set oidset = new java.util.HashSet();
oidset.add(oid);
order.setOrderItemsL(oidset);
session.save(order);
tx.commit();
HibernateUtil.closeSession();
return mapping.findForward(str);

thanks
Rashid
[ December 06, 2006: Message edited by: Bear Bibeault ]
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
OH i forgot to add the hibernate mapping file
here it is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--
- Mapping file for the Hibernate implementation of the Clinic interface.
-->
<hibernate-mapping auto-import="true" default-lazy="false">

<class name="org.springframework.samples.petclinic.Vet" table="vets">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
<set name="specialtiesInternal" table="vet_specialties">
<key column="vet_id"/>
<many-to-many column="specialty_id" class="org.springframework.samples.petclinic.Specialty"/>
</set>
</class>

<class name="org.springframework.samples.petclinic.Specialty" table="specialties">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
</class>

<class name="org.springframework.samples.petclinic.Owner" table="owners">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
<property name="address" column="address"/>
<property name="city" column="city"/>
<property name="telephone" column="telephone"/>
<set name="petsInternal" inverse="true" cascade="all">
<key column="owner_id"/>
<one-to-many class="org.springframework.samples.petclinic.Pet"/>
</set>
</class>

<class name="org.springframework.samples.petclinic.Pet" table="pets">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
<property name="birthDate" column="birth_date" type="date"/>
<many-to-one name="owner" column="owner_id" class="org.springframework.samples.petclinic.Owner"/>
<many-to-one name="type" column="type_id" class="org.springframework.samples.petclinic.PetType"/>
<set name="visitsInternal" inverse="true" cascade="all">
<key column="pet_id"/>
<one-to-many class="org.springframework.samples.petclinic.Visit"/>
</set>
</class>

<class name="org.springframework.samples.petclinic.PetType" table="types">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
</class>

<class name="org.springframework.samples.petclinic.Visit" table="visits">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="date" column="visit_date" type="date"/>
<property name="description" column="description"/>
<many-to-one name="pet" column="pet_id" class="org.springframework.samples.petclinic.Pet"/>
</class>

</hibernate-mapping>
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
MY apologies for messing it up i pasted the wrong file here is the correct file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--
- Mapping file for the Hibernate implementation of the Clinic interface.
-->
<hibernate-mapping auto-import="true" default-lazy="false">


<class name="com.model.Order" table="ras_order">
<id name="order_id" column="order_id" type="int">
<generator class="increment"/>
</id>
<property name="order_name" column="order_name"/>
<set name="orderItemsL" inverse="true" cascade="all">
<key column="order_id"/>
<one-to-many class="com.model.OrderItem"/>
</set>
</class>

<class name="com.model.OrderItem" table="ras_order_item">
<id name="order_item_id" column="order_item_id" type="int">
<generator class="increment"/>
</id>
<property name="order_item_name" column="order_item_name"/>
<many-to-one name="order" column="order_id" class="com.model.Order"/>
</class>


</hibernate-mapping>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem saving with foreign key error
 
Similar Threads
am I doing this correct way?
hibernate parent/child relationship
Struts1: Nested/Indexed properties issue
Problem with Web Service Response
Null Value in @JoinColumn