线程“main”中的异常org.hibernate.AnnotationException:@ org.hibernate.annotations.Table引用一个未知表:ProductForPractice

时间:2016-08-27 19:46:31

标签: java hibernate

我正在尝试使用Hibernate 4.1.4版本功能。当我试图运行代码时,我正在使用hsqldb 2.3.3,它给了我未知的表引用。

这是我的表脚本:

CREATE TABLE ProductForPractice (productId INTEGER,prodName char(25),price DECIMAL(5,2),PRIMARY KEY(productId))

实体类

@Entity

@Table(appliesTo="ProductForPractice")
public class Product {


    @Id
    @Column

    private int productId;



    @Column(name="prodName")
    private String prodName;
    @Column
    private double price;
    @Version
    private long version;
//getter and setters

这是我的配置文件

config.setProperty("hbm2ddl.auto", "create");
config.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
config.setProperty("hibernate.connection.url", "jdbc:hsqldb:hsql://localhost/");
config.setProperty("hibernate.connection.username", "SA");
config.setProperty("hibernate.connection.password", "");
config.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
config.addAnnotatedClass(Product.class);

config.setProperty("hibernate.archive.autodetection", "class,hbm");

ServiceRegistry serivce=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory factory=config.buildSessionFactory(serivce);

这是我的完整堆栈跟踪:

Exception in thread "main" org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unknown table: ProductForPractice
    at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:933)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:713)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3435)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3389)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1731)
    at Hibernate.configuration.Service.saveProduct(Service.java:28)
    at main.Main1.main(Main1.java:15)

1 个答案:

答案 0 :(得分:1)

你的表注释可能属于类org.hibernate.annotations.Table,它只是将信息添加到你应该使用的注释javax.persistence.Table:

@javax.persistence.Table(name="ProductForPractice")
public class Product {