表不存在hibernate

时间:2014-11-07 04:35:07

标签: java mysql sql hibernate

在使用每个超类的hibernate继承表时,我收到一条错误,指出表不存在。我使用mySQL作为数据库,并使用create进行hbmddl2.auto设置。

这是超类:

package solomonadvanced;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity

@Table(name="concre")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Solopercon
{
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
@TableGenerator(name="solol",table="tgfscpc",pkColumnName="pkcn",pkColumnValue="id", valueColumnName="value",allocationSize=1)
@Column(name="s.No")
    int id;
@Column(name="name")
    String name;
    public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
    Solopercon(){}

    }

子类:

package solomonadvanced;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="tapleprderv")
@AttributeOverrides({@AttributeOverride (name="nme",column=@Column(name="name"))})
public class Solotablepersub extends Solopercon{
    @Column(name="salary")
    int salary;
    @Column(name="dept")
    String dept;
    @Column(name="desg")
    String desg;
    @Column(name="hobbies")
    String hobbies;
    public Solotablepersub() {

    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    public String getDesg() {
        return desg;
    }
    public void setDesg(String desg) {
        this.desg = desg;
    }
    public String getHobbies() {
        return hobbies;
    }
    public void setHobbies(String hobbies) {
        this.hobbies = hobbies;
    }



}

实施类:

package solomonadvanced;

import org.hibernate.Session;

public class Solotpcimp {

    public static void main(String[] args) {
        Session sn =Util.getSessionFactory().openSession();
sn.beginTransaction();

        System.out.println("begin transactions");
        Solopercon spc= new Solopercon();
        spc.setName("soloworld");
        sn.save(spc);
        Solotablepersub sts= new Solotablepersub();
        sts.setDept("SWEngg");
        sts.setDesg("Naturalprogrammer");
        sts.setHobbies("reading");
        sts.setSalary(1000);
        sn.save(sts);
        sn.getTransaction().commit();
        sn.close();
        System.out.println("successfully tables were created");         


    }

}

配置文件:

    <?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">
    <hibernate-configuration>
        <session-factory >
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/solomon</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <property name="hibernate.connection.pool_size">15</property>
        <property name="show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
         <mapping class="solomonadvanced.Solohibfirst"/> 
         <mapping class="solomonadvanced.Sologene"/>
         <mapping class="solomonadvanced.Soloatblecreator"/>
         <mapping class="solomonadvanced.Hiberversion"/>
         <mapping class="solomonadvanced.Soloinherabs"/>
         <mapping class="solomonadvanced.Soloprofessional"/>
         <mapping class="solomonadvanced.Solosinpersclss"/>
         <mapping class="solomonadvanced.Solojoinderv"/>
         <mapping class="solomonadvanced.Solopercon"/>
         <mapping class="solomonadvanced.Solotablepersub"/>

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

我收到以下错误:

Nov 07, 2014 10:03:29 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Nov 07, 2014 10:03:29 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Nov 07, 2014 10:03:29 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 07, 2014 10:03:29 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 07, 2014 10:03:29 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 07, 2014 10:03:29 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Nov 07, 2014 10:03:29 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Nov 07, 2014 10:03:29 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 07, 2014 10:03:29 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Nov 07, 2014 10:03:29 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/solomon]
Nov 07, 2014 10:03:29 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root}
Nov 07, 2014 10:03:29 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Nov 07, 2014 10:03:29 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 15 (min=1)
Nov 07, 2014 10:03:30 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Nov 07, 2014 10:03:30 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Nov 07, 2014 10:03:30 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Nov 07, 2014 10:03:30 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: alter table pkjnclmn drop foreign key FK_s76vp6f64k8ec9wn37ym6ednl
Hibernate: drop table if exists concre
Hibernate: drop table if exists details
Hibernate: drop table if exists employeedetails
Hibernate: drop table if exists perclassinheritance
Hibernate: drop table if exists pkjnclmn
Hibernate: drop table if exists sologener
Hibernate: drop table if exists tableg
Hibernate: drop table if exists tapleprderv
Hibernate: drop table if exists updatetble
Hibernate: drop table if exists contact
Hibernate: drop table if exists hibernate_sequences
Hibernate: create table concre (s.No integer not null, name varchar(255), primary key (s.No))
Nov 07, 2014 10:03:32 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table concre (s.No integer not null, name varchar(255), primary key (s.No))
Nov 07, 2014 10:03:32 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Incorrect table name 's'
Hibernate: create table details (fname varchar(255) not null, lname varchar(255), primary key (fname))
Hibernate: create table employeedetails (details varchar(31) not null, id integer not null auto_increment, age varchar(255), name varchar(255), degree varchar(255), designation varchar(255), project varchar(255), salary integer, primary key (id))
Hibernate: create table perclassinheritance (id integer not null auto_increment, dept varchar(255), primary key (id))
Hibernate: create table pkjnclmn (position varchar(255), subject varchar(255), id integer not null, primary key (id))
Hibernate: create table sologener (id int default 1 not null auto_increment, name varchar(255), primary key (id))
Nov 07, 2014 10:03:34 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table sologener (id int default 1 not null auto_increment, name varchar(255), primary key (id))
Nov 07, 2014 10:03:34 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Invalid default value for 'id'
Hibernate: create table tableg (id bigint not null, adress varchar(255), phone integer, primary key (id))
Hibernate: create table tapleprderv (s.No integer not null, name varchar(255), dept varchar(255), desg varchar(255), hobbies varchar(255), salary integer, primary key (s.No))
Nov 07, 2014 10:03:34 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table tapleprderv (s.No integer not null, name varchar(255), dept varchar(255), desg varchar(255), hobbies varchar(255), salary integer, primary key (s.No))
Nov 07, 2014 10:03:34 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Incorrect table name 's'
Hibernate: create table updatetble (id integer not null auto_increment, currstatus varchar(255), vrsn integer, primary key (id))
Hibernate: alter table pkjnclmn add constraint FK_s76vp6f64k8ec9wn37ym6ednl foreign key (id) references perclassinheritance (id)
Hibernate: create table contact ( names varchar(255),  value integer )
Hibernate: create table hibernate_sequences ( sequence_name varchar(255),  sequence_next_hi_value integer )
Nov 07, 2014 10:03:37 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
begin transactions
Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_name = 'concre' for update
Hibernate: insert into hibernate_sequences(sequence_name, sequence_next_hi_value) values('concre', ?)
Hibernate: update hibernate_sequences set sequence_next_hi_value = ? where sequence_next_hi_value = ? and sequence_name = 'concre'
Hibernate: insert into concre (name, s.No) values (?, ?)
Nov 07, 2014 10:03:37 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1146, SQLState: 42S02
Nov 07, 2014 10:03:37 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Table 'solomon.concre' doesn't exist
Nov 07, 2014 10:03:37 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
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.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at solomonadvanced.Solotpcimp.main(Solotpcimp.java:21)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'solomon.concre' 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)
    ... 13 more

1 个答案:

答案 0 :(得分:0)

查看堆栈跟踪,问题是由此无效的列名引起的:

@Column(name="s.No")

尝试更改为:

@Column(name="sNo")

使用反引号引用它也可能有效:

@Column(name="`s.No`")