引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'test.hibernate_sequence'不存在

时间:2016-02-09 17:29:08

标签: java mysql hibernate

我正在处理 Hibernate OneToOne映射示例,并面临以下错误:我不确定这里出了什么问题。请指导。

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    at com.mysql.jdbc.Util.getInstance(Util.java:382)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
    at org.hibernate.id.enhanced.TableStructure.executeQuery(TableStructure.java:224)
    at org.hibernate.id.enhanced.TableStructure.access$300(TableStructure.java:46)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:139)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:126)
    at org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55)
    at org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:56)
    ... 13 more

Vehicle.java

@Entity
@Table(name="VEHICLE")
public class Vehicle {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="VEHICLE_ID")
    private Integer vehicleId;

    @Column(name="VEHICLE_NAME")
    private String vehicleName;
    // setter and getters
}

UserDetails.java

@Entity
@Table(name="USER_DETAIL")
public class UserDetails {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="USER_ID")
    private Integer userId;

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

    @OneToOne
    private Vehicle vehicle;

    // setters and getters
}

冬眠-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/test</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> 

        <!-- Enable Hibernate's automatic session context management -->
          <property name="current_session_context_class">thread</property> 

        <!-- Disable the second-level cache -->
         <property name="cache.provider_class">org.hibernate.cache.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-update</property> 


         <mapping class="com.sdnext.hibernate.tutorial.dto.UserDetails"/>
         <mapping class="com.sdnext.hibernate.tutorial.dto.Vehicle"/>

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

HibernateUtil.java

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } 
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }
}

的pom.xml

<properties>
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Hibernate Entity Manager -->
        <hibernate-entitymanager.version>4.2.8.Final</hibernate-entitymanager.version>
    </properties>

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.7.Final</version>
        </dependency>

        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.15</version>
        </dependency>


        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.20.0-GA</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>

    <!-- Project Build -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
            </plugins>

        </pluginManagement>
    </build>
</project>

MainApp.java

public class MainApp {
    public static void main(String[] args) {
        System.out.println("--- Hibernate One To One mapping ---");
        Session session = HibernateUtil.getSessionFactory().openSession();

        //SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        session.beginTransaction();

        // Vehicle
        Vehicle vehicle = new Vehicle();
        vehicle.setVehicleName("Maruti Car");

        // User Details
        UserDetails userDetails = new UserDetails();
        userDetails.setUserName("First User");
        userDetails.setVehicle(vehicle);

        session.save(vehicle);
        session.save(userDetails);

        session.getTransaction().commit();
        session.close();
    }
}

即使我尝试将@GeneratedValue(strategy=GenerationType.AUTO)更改为@GeneratedValue,但也无效。请帮助解决这个问题。

1 个答案:

答案 0 :(得分:7)

尝试指定hibernate.hbm2ddl.auto=create而不只是hbm2ddl.auto=create-update

这是https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/ch01.html使用的内容,并链接https://developer.jboss.org/thread/176678?tstart=0

并使用<property name="hibernate.id.new_generator_mappings">false</property>