Hibernate表未自动创建

时间:2018-02-20 14:41:22

标签: java mysql hibernate jdbc

我一直在尝试使用Hibernate将我的Java应用程序连接到localhost:3306中的MySQL数据库并创建一个表,但它没有发生。它给出了错误。但是,当我通过给定名称预先创建表格然后运行应用程序时,相同的代码会起作用,它会相应地在表格中插入所有数据。

在hibernate.cfg.xml文件中我试图设置我的
“更新”以更新和创建。当数据库中没有名为“Alien”的表时,它都不起作用。

根据我的理解,当此选项设置为“Update”时,它会创建一个表并插入数据(当表不存在时,它只是在表已存在时插入数据。

请帮我解决这个问题。

以下是我的代码:

pom.xml 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ksk</groupId>
<artifactId>Hibernate Project</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependencies>

   <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>6.0.6</version>
   </dependency>
   <dependency>
       <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->

       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>5.2.12.Final</version>
   </dependency>
   </dependencies>
</project>

的hibernate.cfg.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

    <property name = "hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>

    <!-- Assume test is the database name -->

    <property name = "hibernate.connection.url">
        jdbc:mysql://localhost:3306/myHibernate
    </property>

    <property name = "hibernate.connection.username">
        ksk235
    </property>

    <property name = "hibernate.connection.password">
        password
    </property>
    <property name = "hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <property name="hibernate.show_sql">true</property>

    <property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

Alien.java

package com.ksk;


import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Alien {
@Id
private int aid;
private String aName;
private String color;

public Alien(){};



public int getAid() {
    return aid;
}

public String getaName() {
    return aName;
}

public String getColor() {
    return color;
}

public void setAid(int aid) {
    this.aid = aid;
}

public void setaName(String aName) {
    this.aName = aName;
}

public void setColor(String color) {
    this.color = color;
}
}

App.java

package com.ksk;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class App {

public static void main(String[] args) {

    Alien kaushik =new Alien();
    kaushik.setAid(101);
    kaushik.setaName("Kaushik");
    kaushik.setColor("Green");


    Configuration config = new 
Configuration().configure().addAnnotatedClass(Alien.class);
    SessionFactory sf = config.buildSessionFactory();
    Session session =sf.openSession();

    Transaction tx = session.beginTransaction();
    session.save(kaushik);
    tx.commit();
}

}

我可以在此链接中查看我收到的错误: https://1drv.ms/w/s!AgnhrqMHRiyJjfll32wEkDQRSOQ4Yw

Here's my project structure

1 个答案:

答案 0 :(得分:0)

(1)在档案hibernate.cfg.xml

<property name="hbm2ddl.auto">update</property>

应该是

<property name="hibernate.hbm2ddl.auto">create</property>

(2)您还需要指出文件DDL用于自动化任务,例如

<property name="hibernate.hbm2ddl.import_files">classpath:/WEB-INF/classes/myfolder/myfile.sql</property>

引用

  

文件顺序很重要,之前执行给定文件的语句   以下文件的陈​​述。这些陈述只是   如果创建了模式,则执行,即如果设置了hibernate.hbm2ddl.auto   创建或创建 - 删除。

e.g。 /humans.sql,/dogs.sql

参见参考文件:https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html#configuration-misc-properties