-->
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.  [ 6 posts ] 
Author Message
 Post subject: Field 'ID' doesn't default value
PostPosted: Fri Apr 02, 2010 3:04 am 
Newbie

Joined: Wed Mar 31, 2010 4:35 am
Posts: 19
I have 2 tables like

xydataa and fm4featuress

xydataa.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 19, 2010 4:59:08 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class catalog="navl" name="test.Xydataa" table="xydataa">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="native"/>
</id>
<property name="longi" type="string">
<column name="Longi"/>
</property>
<property name="lat" type="string">
<column name="Lat"/>
</property>
<property name="altitude" type="int">
<column name="Altitude" not-null="true"/>
</property>
<property name="angle" type="int">
<column name="Angle" not-null="true"/>
</property>
<property name="satellite" type="int">
<column name="Satellite" not-null="true"/>
</property>
<property name="speed" type="int">
<column name="Speed" not-null="true"/>
</property>
<property name="objectId" type="int">
<column name="ObjectId" not-null="true"/>
</property>
<property name="clientId" type="int">
<column name="ClientId" not-null="true"/>
</property>
<property name="gpsTime" type="string">
<column length="19" name="GpsTime" not-null="true"/>
</property>
<property name="location" type="string">
<column name="Location"/>
</property>
<property name="city" type="string">
<column name="City"/>
</property>
<property name="state" type="string">
<column name="State"/>
</property>
<property name="distance" type="string">
<column length="100" name="Distance"/>
</property>
<property name="times" type="timestamp">
<column length="19" name="Times"/>
</property>
<many-to-one name="fm4featuress" class="test.Fm4Featuress" cascade="all" insert="false" update="false">
<column name="ID" not-null="false" />
</many-to-one>
</class>
</hibernate-mapping>

fm4featuress.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 19, 2010 4:59:08 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class catalog="navl" name="test.Fm4Featuress" table="fm4featuress">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="native"/>
</id>
<property name="DataId" type="int">
<column name="DataId" not-null="true"/>
</property>
<property name="Value" type="int">
<column name="Value" not-null="true"/>
</property>
<set name="xydataa" table="xydataa" lazy="false">
<key>
<column name="ID" not-null="true"/>
</key>
<one-to-many class="test.Xydataa" />
</set>
</class>
</hibernate-mapping>


pojo class

Xydataa.java

Code:
package test;
// Generated Mar 27, 2010 2:31:16 PM by Hibernate Tools 3.2.1.GA


import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
* Xydata generated by hbm2java
*/
public class Xydataa  implements java.io.Serializable {


     private Integer id;
     private String longi;
     private String lat;
     private int altitude;
     private int angle;
     private int satellite;
     private int speed;
     private int objectId;
     private int clientId;
     private String gpsTime;
     private String location;
     private String city;
     private String state;
     private String distance;
     private Date times;

    public Xydataa() {
    }


    public Xydataa(int altitude, int angle, int satellite, int speed, int objectId, int clientId, String gpsTime, Date times) {
        this.altitude = altitude;
        this.angle = angle;
        this.satellite = satellite;
        this.speed = speed;
        this.objectId = objectId;
        this.clientId = clientId;
        this.gpsTime = gpsTime;
        this.times = times;
    }
    public Xydataa(String longi, String lat, int altitude, int angle, int satellite, int speed, int objectId, int clientId, String gpsTime, String location, String city, String state, String distance, Date times) {
       this.longi = longi;
       this.lat = lat;
       this.altitude = altitude;
       this.angle = angle;
       this.satellite = satellite;
       this.speed = speed;
       this.objectId = objectId;
       this.clientId = clientId;
       this.gpsTime = gpsTime;
       this.location = location;
       this.city = city;
       this.state = state;
       this.distance = distance;
       this.times = times;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    public String getLongi() {
        return this.longi;
    }

    public void setLongi(String longi) {
        this.longi = longi;
    }
    public String getLat() {
        return this.lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }
    public int getAltitude() {
        return this.altitude;
    }

    public void setAltitude(int altitude) {
        this.altitude = altitude;
    }
    public int getAngle() {
        return this.angle;
    }

    public void setAngle(int angle) {
        this.angle = angle;
    }
    public int getSatellite() {
        return this.satellite;
    }

    public void setSatellite(int satellite) {
        this.satellite = satellite;
    }
    public int getSpeed() {
        return this.speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getObjectId() {
        return this.objectId;
    }

    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }
    public int getClientId() {
        return this.clientId;
    }

    public void setClientId(int clientId) {
        this.clientId = clientId;
    }
    public String getGpsTime() {
        return this.gpsTime;
    }

    public void setGpsTime(String gpsTime) {
        this.gpsTime = gpsTime;
    }
    public String getLocation() {
        return this.location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }
    public String getDistance() {
        return this.distance;
    }

    public void setDistance(String distance) {
        this.distance = distance;
    }
    public Date getTimes() {
        return this.times;
    }

    public void setTimes(Date times) {
        this.times = times;
    }

   private Set Fm4Featuress;

   public Set getfm4featuress() {
      return this.Fm4Featuress;
   }

   public void setfm4featuress(Set id) {
      this.Fm4Featuress = Fm4Featuress;
   }

   // helper method to maintain bi-directional relationship
   public void addFm4Feature(Fm4Featuress fm4featuress) {
      this.Fm4Featuress.add(fm4featuress);
      fm4featuress.setXydataa(this);
   }

 
//Fm4Featuress fm4featuress;
//    public Fm4Featuress getFm4Featuress() {
//        return this.fm4featuress;
//   }

//   public void setFm4Featuress(Fm4Featuress fm4featuress) {
//       this.fm4featuress = fm4featuress;
//   }

//-------------------------------------------
  // public Set getfm4featuress() {
   //   return this.fm4featuress;
//   }

  //  public void setfm4featuress(Set fm4featuress) {
//      this.fm4featuress =  fm4featuress;
//   }


}


and
Code:
Fm4featuress.java

package test;
// Generated Mar 19, 2010 4:59:06 PM by Hibernate Tools 3.2.1.GA


import java.util.Date;

public class Fm4Featuress  implements java.io.Serializable {


    private Integer id;
    private Integer DataId;
    private Integer Value;


     public Fm4Featuress()
   {
    }

   public Fm4Featuress(int id, int DataId, int Value)
   {
        this.id = id;
        this.DataId = DataId;
        this.Value = Value;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    public void setDataId(Integer DataId) {
        this.DataId = DataId;
    }
        public Integer getDataId() {
        return this.Value;
    }

    public void setValue(Integer Value) {
        this.Value = Value;
    }

        public Integer getValue() {
        return this.Value;
    }

private Xydataa xydata;

   public Xydataa getXydataa() {
      return this.xydata;
   }

   public void setXydataa(Xydataa xydata) {
      this.xydata = xydata;
   }




//private Xydataa xydataa = null;



//Xydataa xydataa;
//public Xydataa getXydataa() {
//      return xydataa;
//   }

//   public void setXydataa(Xydataa xydataa) {
//      this.xydataa = xydataa;
//   }
 
//  public Set getXydataa() {
//      return this.xydataa;
//   }

//   public void setXydataa(Set xydataa) {
//      this.xydataa = xydataa;
//   }

// helper method to maintain bi-directional relationship
//  public void addXydataa(Xydataa xy) {
//      this.xydataa.add(xy);
//      xydataa.setFm4Featuress(this);
//   }




}



The main file

Code:
{
//    PreparedStatement preparedStatement = null;
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    DecimalNumber xx ;
    DecimalNumber yy ;
    Session sess = null;
    Transaction tr = null;
    sess = HibernateUtil.getSessionFactory().openSession();
      try
        {
          tr = sess.beginTransaction();
           Date resultdate = new Date(times);
           xx = new DecimalNumber(x,7);
           yy = new DecimalNumber(y,7);
           System.out.println("Inserting xydataa");
           Xydataa xy=new Xydataa();
           Fm4Featuress fm = new Fm4Featuress();
           Set fm1 = new HashSet();
           //xy.setId(null);
           xy.setLongi(xx+"");
           xy.setLat(yy+"");
           xy.setAltitude(altitude);
           xy.setAngle(angle);
           xy.setSatellite(satellites);
           xy.setSpeed(speed);
           xy.setObjectId(objectId);
           xy.setClientId(clientId);
           xy.setGpsTime(sdf.format(resultdate));
           xy.setLocation(location);
           xy.setCity(city);
           xy.setState(state);
           xy.setDistance(distance);
           sess.save(xy);
           System.out.println("Done");
           tr.commit();
         
         
           for (int i = 0; i < obj.length && obj[i] != 0; i++)
          {
           sess = HibernateUtil.getSessionFactory().openSession();
           tr = sess.beginTransaction();
           int all = obj[i] - 1;
           System.out.println("Inserting Fm4Featuress");
           //int b=xy.getId();
           //fm.setId(b);
           fm.setDataId(all);
           fm.setValue(obja[i]);
           fm1.add(fm);
           xy.setfm4featuress(fm1);
           //sess.save(xy);
           //sess.flush();
           //xy=(Xydataa)sess.merge(fm);
         //  xy.getfm4featuress();
         //  fm.getXydataa();
           sess.save(fm);
           //sess.flush();
           tr.commit();
          }
        System.out.println("Commit");
        }catch(HibernateException exx)
        {
         Logger.getLogger(Alldatabase.class.getName()).log(Level.SEVERE, null, exx);

        }finally
        {
         sess.close();
         }
}



i got the following error

INFO: Inserting xydataa
INFO: Hibernate: insert into navl.xydataa (Longi, Lat, Altitude, Angle, Satellite, Speed, ObjectId, ClientId, GpsTime, Location, City, State, Distance, Times) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
INFO: Done
INFO: Inserting Fm4Featuress
INFO: Hibernate: insert into navl.fm4featuress (DataId, Value) values (?, ?)
WARNING: SQL Error: 1364, SQLState: HY000
SEVERE: Field 'ID' doesn't have a default value
SEVERE: null
org.hibernate.exception.GenericJDBCException: could not insert: [test.Fm4Featuress]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at tcplistener.Alldatabase.Database(Alldatabase.java:175)
at tcplistener.ModuleHandler.run(ModuleHandler.java:155)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2398)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2316)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2301)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 18 more


i am going get the last insert id of xydataa and put in to the fm4featuress at the same time
please help me what i am doing wrong


Top
 Profile  
 
 Post subject: Re: Field 'ID' doesn't default value
PostPosted: Fri Apr 02, 2010 3:56 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
It seems that the MySql database for some reason doesn't recognize that he itself should assign the id numbers
(generator class="native"). As nobody sets the id value and as in the schema there's no default value defined for the id column,
the exception raises.

Can it be that you missed to regenerate the schema on database after modifying your hbm.xml files ?
Please verify the schema definition and compare the difference of xydataa.id definition and fm4featuress.id definition.


Top
 Profile  
 
 Post subject: Re: Field 'ID' doesn't default value
PostPosted: Fri Apr 02, 2010 5:00 am 
Newbie

Joined: Wed Mar 31, 2010 4:35 am
Posts: 19
pb00067 wrote:
It seems that the MySql database for some reason doesn't recognize that he itself should assign the id numbers
(generator class="native"). As nobody sets the id value and as in the schema there's no default value defined for the id column,
the exception raises.

Can it be that you missed to regenerate the schema on database after modifying your hbm.xml files ?
Please verify the schema definition and compare the difference of xydataa.id definition and fm4featuress.id definition.



I am using (generator="native") without receating the schema how to solve the problem and let me know any generator is there,

I am using one to many and many to one mapping to get the last insert id from the xydataa and put into the fm4featuress,

If u know any other way(mapping) to get the last insert id from the xydataa and put into the fm4featuress,

Please help me,


Top
 Profile  
 
 Post subject: Re: Field 'ID' doesn't default value
PostPosted: Fri Apr 02, 2010 5:01 am 
Newbie

Joined: Wed Mar 31, 2010 4:35 am
Posts: 19
I am using (generator="native") without receating the schema how to solve the problem and let me know any generator is there,

I am using one to many and many to one mapping to get the last insert id from the xydataa and put into the fm4featuress,

If u know any other way(mapping) to get the last insert id from the xydataa and put into the fm4featuress,

Please help me,


Top
 Profile  
 
 Post subject: Re: Field 'ID' doesn't default value
PostPosted: Fri Apr 02, 2010 5:20 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
You cannot expect that your Hiberante application continues to work if you change mappings in the hbm-files without
updating the schema on the database.
In order to see of your current schema is aligned to your hbm files,
please verify the schema definition on database and compare the difference of xydataa.id definition and fm4featuress.id definition.
if your schema is up-todate, there should be no difference.
It there is a difference then you must issue an alter column command adapting fm4featuress.id column
to the features which xydataa.id has.

The schema on MySql should be possible to view with a MySql tool, otherwise you can also use Squirrel.


Top
 Profile  
 
 Post subject: Re: Field 'ID' doesn't default value
PostPosted: Fri Apr 02, 2010 5:29 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
and let me know any generator is there,


yes, there is a effectively a native generator there on MySql, otherwise you would have got an error previously
because hibernate checks this ability through the jdbc-driver
Code:
connection.getMetadata().supportsGetGeneratedKeys() // return true  if auto-generated keys can be retrieved after a statement has executed;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.