tengguanbing
2010-10-29 09:10
浏览 403
已采纳

为什么hibernate不能自动建表

hibernate.cfg.xml
[code="java"]
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<session-factory>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">Create</property>
    <property name="current_session_context_class">thread</property>

    <mapping class="com.jjn.hibernate.bean.Cat"/>
</session-factory>


[/code]

Cat.java
[code="java"]
package com.jjn.hibernate.bean;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="Cat")
public class Cat {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@Column(name="name")
private String name;

@Column(name="decription")
private String decription;

@ManyToOne
@JoinColumn(name="mother_id")
private Cat mother;

@Temporal(TemporalType.TIMESTAMP)
@Column(name="createDate")
private Date createDate;

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDecription() {
    return decription;
}
public void setDecription(String decription) {
    this.decription = decription;
}
public Cat getMother() {
    return mother;
}
public void setMother(Cat mother) {
    this.mother = mother;
}
public Date getCreateDate() {
    return createDate;
}
public void setCreateDate(Date createDate) {
    this.createDate = createDate;
}

}

[/code]
CatTest.java
[code="java"]
package com.jjn.hibernate.test;

import java.awt.Font;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jjn.hibernate.bean.Cat;
import com.jjn.hibernate.util.HibernateUtil;

public class CatTest {
public static void main(String[] args) {
Cat mother=new Cat();
mother.setName("Mary White");
mother.setDecription("The Mama Cat……");
mother.setCreateDate(new Date());

    Cat kitty=new Cat();
    kitty.setMother(mother);
    kitty.setName("Kitty");
    kitty.setDecription("Hello Kitty");
    kitty.setCreateDate(new Date());

    Cat mimmy=new Cat();
    mimmy.setMother(mother);
    mimmy.setName("Mimmy");
    mimmy.setDecription("Kitty's little twin sister....");
    mimmy.setCreateDate(new Date());

    Session session=HibernateUtil.getSessionFactory().openSession();
    Transaction transn=session.beginTransaction();

    session.persist(mother);
    session.persist(kitty);
    session.persist(mimmy);
    @SuppressWarnings("all")
    List<Cat> catList=session.createQuery("from Cat").list();

    StringBuffer result=new StringBuffer();
    result.append("数据库里所有的猫:");

    for(Cat cc:catList){
        result.append("猫:"+cc.getName()+",");
        result.append("猫妈妈:"+(cc.getMother()==null?"没有记录":cc.getMother().getName()));
        result.append("\r\n");
    }
    transn.commit();
    session.close();
    JOptionPane.getRootFrame().setFont(new Font("Arial",Font.BOLD,14));
    JOptionPane.showMessageDialog(null, result.toString());
}

}

[/code]
我运行CatTest.java就报错
[code="java"]
Hibernate: insert into Cat (createDate, decription, mother_id, name) values (?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.jjn.hibernate.bean.Cat]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:646)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:620)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:624)
at com.jjn.hibernate.test.CatTest.main(CatTest.java:38)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'hibernate.cat' doesn't exist
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 14 more

[/code]

请各位帮我看看

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • lizhiyezi 2010-10-29 10:57
    已采纳

    [code="java"]create[/code]
    Create 小写

    已采纳该答案
    打赏 评论
  • enet_java 2010-10-29 09:20

    将Cat修改为 com.jjn.hibernate.bean.Cat 试试。

    打赏 评论
  • enet_java 2010-10-29 09:22

    [quote]将Cat修改为 com.jjn.hibernate.bean.Cat 试试。[/quote]

    确认一下用户hibernate下是否存在表cat呀?

    打赏 评论
  • enet_java 2010-10-29 09:24

    hibernate 不能够自动创建数据库表的,hibernate是完成从数据库表到持久化bean的一个映射。

    打赏 评论
  • oyj7454103 2010-10-29 09:24
    打赏 评论
  • zgy52188 2010-10-29 09:46

    你是想在一张表中表现出猫妈妈 和 猫 的关系,说白了就是一个tree,
    我想是不是你@ManyToOne错了? 如果有两张表的话去@ManyToOne应该一起,互相能建立关系,一张表就不用了吧。

    打赏 评论
  • fxmabo 2010-10-29 09:48

    你应该是没有在那个Cat里面写构造方法,四个参数的,数据库无法识别![code="java"]insert into Cat (createDate, decription, mother_id, name) values (?, ?, ?, ?) [/code]这里调用了Cat的四个参数的构造,你没写,所以找不到,就出错了 :P 。

    打赏 评论

相关推荐 更多相似问题