These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Can't get the mapping to work with one-to-may relation
PostPosted: Sat Jul 22, 2006 3:50 pm 
Newbie

Joined: Sat Jul 22, 2006 3:08 pm
Posts: 6
I've tried everything and searched the forum and.
I can't get it right (may by I don't understand the whole concept)
I can't add a car to client.
The car data is filled out by user on the web page and client is already in the database.
And I get "Field 'id' doesn't have a default value"
I set id to <id name="carId"
column="id">
<generator class="native"/>

I don't get it ???
Please point me in the right direction.

public class Client extends ActionForm {

private int klient_id;
private String name;
private String surname;
private String region;
private int age;
private boolean male;
private Set cars = new HashSet();
}
public class CarBean extends ActionForm {
private int carId;
private int klient_id;
private String make;
private String type;
private int year;
private String distance;
private String capacity;
}


Hibernate version:3.0

Mapping documents:
<hibernate-mapping package="pl.axmar" default-lazy="false">

<class name="Client" table="klient">
<comment>Lista klientow</comment>
<id name="klient_id"
column="id">
<generator class="native"/>
</id>
<property name="name" column="imie"/>
<property name="surname" column="nazwisko"/>
<property name="region" column="region"/>
<property name="age" column="wiek"/>
<property name="male" column="mezczyzna"/>
<set name="cars">
<key column="klient_id"/>
<one-to-many class="CarBean"/>
</set>

</class>

</hibernate-mapping>
<hibernate-mapping package="pl.axmar">

<class name="CarBean" table="pojazd">
<id name="carId"
column="id">
<generator class="native"/>
</id>
<property name="make" column="marka"/>
<property name="type" column="typ"/>
<property name="year" column="rok"/>
<property name="distance" column="przebieg"/>
<property name="capacity" column="pojemnosc"/>
</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Session ses= HibernateUtil.getSessionFactory().getCurrentSession();
ses.beginTransaction();
CarBean car=(CarBean)actionForm;
ses.save(car);

Client cl= (Client) ses.load(Client.class,car.getKlient_id());
cl.getCars().add(car);

ses.getTransaction().commit();


Full stack trace of any exception that occurs:
Field 'id' doesn't have a default value


Name and version of the database you are using:MySQL

The generated SQL (show_sql=true):
Hibernate: insert into pojazd (marka, typ, rok, przebieg, pojemnosc) values (?, ?, ?, ?, ?)


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 22, 2006 6:32 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Is 'id' column of table 'pojazd' AUTO_INCREMENT?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 22, 2006 6:45 pm 
Newbie

Joined: Sat Jul 22, 2006 3:08 pm
Posts: 6
Thanx it does work with auto increment :-) BUT

I thought that if I set
<id name="carId" column="id">
<generator class="native"/>
</id>

generator to native it will take care of it for me.
Should I set generator to something else if I don't want to
set id to autoincrement ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 22, 2006 7:05 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Hibernate Reference Documentation
Chapter 5. Basic O/R Mapping
5.1. Mapping declaration
5.1.4. id
Generator

  • increment
  • identity
  • sequence
  • hilo
  • seqhilo
  • uuid
  • guid
  • native
  • assigned
  • select
  • foreign


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.