This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

problem saving with foreign key error  RSS feed

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!