org.hibernate.exception.SQLGrammarException:hibernate不会加入列

时间:2014-10-01 10:55:21

标签: java hibernate jpa jboss7.x mysql-connector

我遇到了hibernate的问题...过去两天我已经尝试过所有的事情来解决这个问题了,我没有更多的想法......

我有一个Mysql 5.0数据库,并从实体中创建了类。 其中两个:

@Entity
@Table(name="products")
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long idProduct;


    @OneToOne()
//  @JoinTable(name="Tblmodeli")
//  @JoinColumn(name="idModel") These two lines are important

    @NotFound(action = NotFoundAction.EXCEPTION)
    private Model idmodel;

    public Long getidProduct() {
        return this.idProduct;
    }

    public void setidProduct(Long idProduct) {
        this.idProduct= idProduct;
    }
public Model getIdmodel() {
        return this.idmodel;
    }

    public void setIdmodel(Model idmodel) {
        this.idmodel = idmodel;
    }
}

第二张表:

@Entity
@Table(name="model")
@NamedQuery(name="model.findAll", query="SELECT t FROM model t")
public class Model implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="idModel")
    private Long idModel;

private String name;

    public Long getIdModel() {
        return this.idModel;
    }

    public void setIdModel(Long idModel) {
        this.idModel = idModel;
    }

     public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name= name;
    }
}

persistance.xml

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="SomeName" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/mydb/</jta-data-source>
<class>model.Products</class> 
<class>model.Model</class>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql"/>
<property name="hibernate.max_fetch_depth" value="4"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://MyLink"/>
<property name="javax.persistence.jdbc.user" value="username"/>
<property name="javax.persistence.jdbc.url.password" value="password"/>

当我发出此命令时

Product p=  em.find(Product.class, 1L);
String s=p.getIdmodel().getName();

我在控制台中成为了这个:

  select
product0_.IDNormativa as idProduct1_806_,
product0_.idmodel_idModel as idmodel59_806_
 from
 products product0_ 
where
product0_.productID = '1'

12:19:38,293 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-2) SQL Error: 1054, SQLState: 42S22
12:19:38,293 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-2) Unknown column 'product0_.idmodel_idModel' in 'field list'
12:19:38,294 INFO  [stdout] (http-localhost-127.0.0.1-8080-2) javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Unknown column 'product0_.idmodel_idModel' in 'field list'

当我取消注释这两行// @JoinTable(name =&#34; Tblmodeli&#34;)// @JoinColumn(name =&#34; idModel&#34;)我成为了这个:

  select
product0_.IDNormativa as idProduct1_806_,
product0_.idmodel_idModel as idmodel59_806_
 from
 products product0_ 
left outer join
Model product0_1_ 
on product0_.idProduct=product0_1_.idProduct
//In this line i need  on product0_.idmodel=product0_1_.idModel
    where
    product0_.idProduct= '1'

12:27:35,546 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) SQL Error: 1054, SQLState: 42S22
12:27:35,546 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) Unknown column 'product0_1_.idProduct' in 'on clause'
12:27:35,547 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Unknown column 'product0_1_.idProduct' in 'on clause'

我试过这些方言:

<!--<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>  -->
<!--<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>  -->
<!--<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> -->

我有mysql-connector-5.1.17.jar 该项目部署在Jboss AS 7.1.1上 我在同一个项目中创建了一些OneToOne和OneToMany,所有这些都在工作,除了这个和几个mnore ...在JBOSS文件夹中我有连接器,在standalone.xml中有数据库和连接器

<datasources>
   <datasource jta="true" jndi-name="java:/mydb/" pool-name="my_pool" enabled="true" use-java-context="true" use-ccm="true">
          <connection-url>jdbc:mysql://MyURI</connection-url>
       <driver>mysql</driver>
    <security>
     <user-name>username</user-name>
     <password>password</password>
       </security>
    <statement>
   <prepared-statement-cache-size>100</prepared-statement-cache-size>
          <share-prepared-statements>true</share-prepared-statements>
    </statement>
     </datasource>
   <drivers>
        <driver name="mysql" module="com.mysql"/>
         <driver name="h2" module="com.h2database.h2">
            <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
       </driver>
      </drivers>
    </datasources>

出于某种原因,hibernate不会加入产品和模型表...... 有人能帮我吗? Thaks ...

3 个答案:

答案 0 :(得分:0)

不确定是否修复了它,但类名应该是Model而不是Models。

@NotFound(action = NotFoundAction.EXCEPTION)
private Model**s** idmodel;

答案 1 :(得分:0)

您错过hibernate.hbm2ddl.auto中的persistence.xml财产。 property标记也应位于properties标记内。例如:

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="SomeName" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/mydb/</jta-data-source>

    <class>model.Products</class> 
    <class>model.Model</class>
    <properties> <!-- **** -->
        <!-- *** hbm2ddl.auto values: 'validate', 'update', 'create', 'create-drop' -->
        <property name="hibernate.hbm2ddl.auto" value="update"/> <!-- *** -->
        <property name="hibernate.format_sql" value="true"/>
        ....
        ....

hibernate.hbm2ddl.autocould be

  • create:在每次运行中构建一个新数据库
  • update:修改现有数据库
  • create-drop:与&#34;创建&#34;相同但在Hibernate关闭时也会丢弃表格
  • validate:不对数据库进行任何更改
祝你好运!

答案 2 :(得分:0)

你的某人pojo / entity的字段没有出现在daoImpl的sql中,所以添加它。