2014-09-23 51 views
-1

我是hibernate的新手,我遇到了一些小問題,我也發佈了代碼,希望得到所有人的建議有經驗的人在那裏。我也發佈了我收到的錯誤。在創建一個表時它已經正常工作了,但之前工作正常,但後來遇到了一些probs

這是我的模型類

package com.hibernate.arjun; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

@Entity 
public class Car_Info { 

    @Column(name = "CarName") 
    private String car_name; 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "CarNo") 
    private String car_no; 
    @Column(name = "CarModel") 
    private String car_model; 

    public String getCar_name() { 
     return car_name; 
    } 

    public void setCar_name(String car_name) { 
     this.car_name = car_name; 
    } 

    public String getCar_no() { 
     return car_no; 
    } 

    public void setCar_no(String car_no) { 
     this.car_no = car_no; 
    } 

    public String getCar_model() { 
     return car_model; 
    } 

    public void setCar_model(String car_model) { 
     this.car_model = car_model; 
    } 
} 

這是我的主類

package com.hibernate.arjun; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 

public class MainCar { 

    public static void main(String args[]) { 
     Car_Info car_Info = new Car_Info(); 
     car_Info.setCar_name("Ferrari"); 
     car_Info.setCar_model("Italian"); 


     SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory(); 
     Session session = factory.openSession(); 
     session.beginTransaction(); 

     session.save(car_Info); 
     session.getTransaction().commit(); 
     session.close(); 
     factory.close(); 
    } 
} 

,這是我hibernate.cfg.xml

<hibernate-configuration> 

<session-factory> 

    <!-- Database connection settings --> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/CarTable</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">root</property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 

    <!-- Drop and re-create the database schema on startup --> 
    <property name="hbm2ddl.auto">create</property> 


    <mapping class="com.hibernate.arjun.Car_Info" /> 

</session-factory> 
</hibernate-configuration> 

我米只是CRE從休眠表中的哪三列,但然後這是我遇到的錯誤

Sep 23, 2014 5:02:36 PM org.hibernate.tool.hbm2ddl.SchemaExport execute 
INFO: HHH000227: Running hbm2ddl schema export 
Hibernate: drop table if exists Car_Info 
Hibernate: create table Car_Info (CarNo varchar(255) not null auto_increment, CarModel varchar(255), CarName varchar(255), primary key (CarNo)) 
Sep 23, 2014 5:02:36 PM org.hibernate.tool.hbm2ddl.SchemaExport perform 
ERROR: HHH000389: Unsuccessful: create table Car_Info (CarNo varchar(255) not null auto_increment, CarModel varchar(255), CarName varchar(255), primary key (CarNo)) 
Sep 23, 2014 5:02:36 PM org.hibernate.tool.hbm2ddl.SchemaExport perform 
ERROR: Incorrect column specifier for column 'CarNo' 
Sep 23, 2014 5:02:36 PM org.hibernate.tool.hbm2ddl.SchemaExport execute 
INFO: HHH000230: Schema export complete 
Hibernate: insert into Car_Info (CarModel, CarName) values (?, ?) 
Sep 23, 2014 5:02:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
WARN: SQL Error: 1146, SQLState: 42S02 
Sep 23, 2014 5:02:36 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
ERROR: Table 'cartable.car_info' doesn't exist 
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement 
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80) 
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) 
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211) 
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96) 
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032) 
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558) 
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98) 
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490) 
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195) 
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179) 
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214) 
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209) 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194) 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702) 
    at com.hibernate.arjun.MainCar.main(MainCar.java:19) 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'cartable.car_info' doesn't exist 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408) 
    at com.mysql.jdbc.Util.getInstance(Util.java:383) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1062) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4208) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4140) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2597) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2826) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262) 
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246) 
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) 
    ... 22 more 

我還有一個疑問。是否有必要在MySql中創建一個表格,因爲當我在cfg.xml文件中使用create時它會給我錯誤,它會自動刪除表格,我該怎麼辦?

謝謝!

+0

<屬性名=「hbm2ddl.auto」>創建將其設置爲更新和嘗試 – 2014-09-23 12:11:31

回答

2

錯誤說

Incorrect column specifier for column 'CarNo' 

,因爲你已經聲明的字段爲String,而不是int

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "CarNo") 
private String car_no; 

正如你所說@GeneratedValue(strategy = GenerationType.AUTO)休眠假定場car_no的數量和試圖建立您的表格爲:

CarNo varchar(255) not null auto_increment 

在這裏您可以清楚地看到,休眠正試圖包含auto_increment,這對於String數據類型是不正確的。因此,創建表本身失敗,出現錯誤:

ERROR: HHH000389: Unsuccessful: create table Car_Info (CarNo varchar(255) not null auto_increment, CarModel varchar(255), CarName varchar(255), primary key (CarNo)) 

要解決該問題,你的字段更改爲任何數據類型一樣intlong等,其中auto_increment適用。

如果你仍然想使用car_noString,那麼你需要使用GenerationType.ASSIGNED生成策略這是任何標識的默認策略,所以只要去掉代戰略意味着調用session.save(Object)之前,你需要在你的代碼手動分配標識方法。

@Id 
//@GeneratedValue(strategy = GenerationType.ASSIGNED) -- this line is optional 
@Column(name = "CarNo") 
private String car_no; 

關於財產hbm2ddl.auto,當其值設置爲create然後自動休眠運行drop命令,然後創建表在創建SessionFactory的。如果您不希望每次運行應用程序時都刪除表,則必須將其值設置爲update

參考此鏈接,詳細瞭解就是hbm2ddl - Hibernate hbm2ddl.auto possible values and what they do?

+0

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表「cartable.car_info」不存在 – 2014-09-23 12:28:15

+0

@SamwiseGamgee,做什麼你的意思是? – Chaitanya 2014-09-23 12:31:19

+0

這就是錯誤看他發佈的日誌。 – 2014-09-23 12:51:19

0

@GeneratedValue(strategy = GenerationType.AUTO)不能被用來產生string ID。 如果你想使用string編號,然後只把@Id超過private String car_no;一樣,

@Id 
    @Column(name = "CarNo") 
    private String car_no; 

car_no的數據類型,否則更改爲int

+0

Thanx很多答覆和yea我的壞,我明白我的錯誤將保持這是我的想法 – 2014-09-23 13:16:39

相關問題