1

I have a problem with hibernate... I have tried all things to solve this past two days and I have no more ideas...

I have a Mysql 5.0 database and made classes from entities. Two of them:

@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;
    }
}

The second table:

@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"/>

When I make this command

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

I become this in console:

  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'

When i Uncomment this two lines // @JoinTable(name="Tblmodeli") // @JoinColumn(name="idModel") I become this:

  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'

I have tried these dialects:

<!--<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"/> -->

I have mysql-connector-5.1.17.jar The project is deployed on Jboss AS 7.1.1 I made some OneToOne and OneToMany in same project and all of them are working except this and few mnore... In the JBOSS folder i have the connector and in the standalone.xml the database and connector

<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>

For some reason hibernate don't join product and model tables... Can someone help me? Thaks...

xXxJAVAxXx
  • 15
  • 1
  • 1
  • 6

3 Answers3

0

Not sure if that fixes it all but the class name should be Model and not Models.

@NotFound(action = NotFoundAction.EXCEPTION)
private Model**s** idmodel;
Juru
  • 1,623
  • 17
  • 43
0

You miss hibernate.hbm2ddl.auto property in persistence.xml. Also property tags should be inside a properties tag. By example:

<?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.auto values could be:

  • create: build a new database on each run
  • update: modify an existing database
  • create-drop: means the same as "create" but also drops tables when Hibernate closes
  • validate: makes no changes to the database

Good luck!

jmvivo
  • 2,653
  • 1
  • 16
  • 20
  • I cant use this in production hbm2ddl.auto values: by this topic http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do – xXxJAVAxXx Oct 01 '14 at 11:18
  • I every time make all changes in the database and make entities from tables whit JPA plugin for eclipse... – xXxJAVAxXx Oct 01 '14 at 11:21
  • Ok, Sorry, I misunderstood the question. – jmvivo Oct 01 '14 at 11:33
0

your someone pojo/entity's field is not appear in daoImpl's sql ,so add it.

jizhuolin
  • 1
  • 2