奇怪的生成了hibernate的sql语句

时间:2014-02-04 00:17:08

标签: sql hibernate

这就是我试图做的事情

session.get(Company.class, 1);

Hibernate: 
    select
    company0_.Company_ID as Company1_31_4_,
    company0_.version as version2_31_4_,
    company0_.Address_ID as Address5_31_4_,
    company0_.Company_Code as Company3_31_4_,
    company0_.Company_Name as Company4_31_4_,
    company0_.Phone_ID as Phone6_31_4_,
    address1_.Address_ID as Address1_0_0_,
    address1_.version as version2_0_0_,
    address1_.Comapny_ID as Comapny4_0_0_,
    address1_.Address as Address3_0_0_,
    company2_.Company_ID as Company1_31_1_,
    company2_.version as version2_31_1_,
    company2_.Address_ID as Address5_31_1_,
    company2_.Company_Code as Company3_31_1_,
    company2_.Company_Name as Company4_31_1_,
    company2_.Phone_ID as Phone6_31_1_,
    phone3_.Phone_ID as Phone1_77_2_,
    phone3_.version as version2_77_2_,
    phone3_.Comapny_ID as Comapny5_77_2_,
    phone3_.Fax_Number as Fax3_77_2_,
    phone3_.Phone_Number as Phone4_77_2_,
    company4_.Company_ID as Company1_31_3_,
    company4_.version as version2_31_3_,
    company4_.Address_ID as Address5_31_3_,
    company4_.Company_Code as Company3_31_3_,
    company4_.Company_Name as Company4_31_3_,
    company4_.Phone_ID as Phone6_31_3_ 
from
    greentk_final.Company company0_ 
left outer join
    greentk_final.Address address1_ 
        on company0_.Address_ID=address1_.Address_ID 
left outer join
    greentk_final.Company company2_ 
        on address1_.Comapny_ID=company2_.Company_ID 
left outer join
    greentk_final.Phones phone3_ 
        on company0_.Phone_ID=phone3_.Phone_ID 
left outer join
    greentk_final.Company company4_ 
        on phone3_.Comapny_ID=company4_.Company_ID 
where
    company0_.Company_ID=?

Feb 04, 2014 1:58:11 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
Feb 04, 2014 1:58:11 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'address1_.Comapny_ID' in 'field list'
Feb 04, 2014 1:58:11 AM org.hibernate.event.internal.DefaultLoadEventListener onLoad
INFO: HHH000327: Error performing load command :     org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not     extract ResultSet
    at   org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:61)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2036)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815)
at org.hibernate.loader.Loader.doQuery(Loader.java:899)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2117)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3927)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:460)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:429)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:206)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:145)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1092)
at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1007)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:173)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285)
at org.hibernate.engine.internal.StatefulPersistenceContext.unproxyAndReassociate(StatefulPersistenceContext.java:677)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:90)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:957)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:936)
at main.Main.main(Main.java:53)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'address1_.Comapny_ID' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)
    ... 24 more

package model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import model.calibration.AbstractModel;

/**
* 
* @author Islam Morad
*/
@Entity
@Table(name = "Address")
public class Address extends AbstractModel implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 7162754693551897604L;
    private long addressID;
    private String street;
    private Company company;

    /**
     * @return the addressID
     */
    @Id
    @GeneratedValue
    @Column(name = "Address_ID", nullable = false)
    public long getAddressID() {
        return addressID;
    }

    /**
     * @param addressID
     *            the addressID to set
     */
    public void setAddressID(long addressID) {
        this.addressID = addressID;
    }

    /**
     * @return the street
     */
    @Column(name = "Address", nullable = false)
    public String getStreet() {
        return street;
    }

    /**
     * @param street
     *            the street to set
     */
    public void setStreet(String street) {
        this.street = street;
    }

    /**
     * @return the company
     */
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "Comapny_ID")
    public Company getCompany() {
        return company;
    }

    /**
     * @param company
     *            the company to set
     */
    public void setCompany(Company company) {
        this.company = company;
    }
}

这是完整的堆栈跟踪

Feb 06, 2014 9:20:26 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Feb 06, 2014 9:20:27 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Feb 06, 2014 9:20:27 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 06, 2014 9:20:27 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Feb 06, 2014 9:20:27 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /main/hibernate.cfg.xml
Feb 06, 2014 9:20:27 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /main/hibernate.cfg.xml
Feb 06, 2014 9:20:28 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: GreenTK
Feb 06, 2014 9:20:29 AM        org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl     configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Feb 06, 2014 9:20:29 AM       org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl   configure
INFO: HHH000115: Hibernate connection pool size: 20
Feb 06, 2014 9:20:29 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Feb 06, 2014 9:20:29 AM     org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl     configure
    INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL     [jdbc:mysql://localhost:3306/testing]
    Feb 06, 2014 9:20:29 AM     org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl     configure
    INFO: HHH000046: Connection properties: {user=root, password=****}
    Feb 06, 2014 9:20:32 AM org.hibernate.dialect.Dialect <init>
    INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
     Feb 06, 2014 9:20:34 AM       org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
      INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
      Feb 06, 2014 9:20:34 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
      INFO: HHH000397: Using ASTQueryTranslatorFactory
      Feb 06, 2014 9:20:34 AM org.hibernate.search.Version <clinit>
      INFO: HSEARCH000034: Hibernate Search 4.3.0.Final
      Feb 06, 2014 9:20:34 AM org.hibernate.search.impl.ConfigContext getLuceneMatchVersion
      WARN: HSEARCH000075: Configuration setting hibernate.search.lucene_version was not            specified, using LUCENE_CURRENT.
      Feb 06, 2014 9:20:38 AM org.hibernate.internal.SessionFactoryRegistry addSessionFactory
      WARN: HHH000277: Could not bind factory to JNDI
      org.hibernate.service.jndi.JndiException: Error parsing JNDI name [GreenTK]
      at     o    rg.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:92)
         org.hibernate.service.jndi.internal.JndiServiceImpl.bind(JndiServiceImpl.java:108)
      at         org.hibernate.internal.SessionFactoryRegistry.addSessionFactory(SessionFactoryRegistry.java:       89)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:480)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at main.HibernateUtil.<clinit>(HibernateUtil.java:28)
    at main.Main.main(Main.java:48)
       Caused by: javax.naming.NoInitialContextException: Need to specify class name in         environment or system property, or as an applet parameter, or in an application resource           file:  java.naming.factory.initial
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
      at javax.naming.InitialContext.getNameParser(InitialContext.java:499)
      at          org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:86)
       ... 7 more


Hibernate: 
    select
    company0_.Company_ID as Company1_31_4_,
    company0_.version as version2_31_4_,
    company0_.Address_ID as Address5_31_4_,
    company0_.Company_Code as Company3_31_4_,
    company0_.Company_Name as Company4_31_4_,
    company0_.Phone_ID as Phone6_31_4_,
    address1_.Address_ID as Address1_0_0_,
    address1_.version as version2_0_0_,
    address1_.Company_ID as Company4_0_0_,
    address1_.Address as Address3_0_0_,
    company2_.Company_ID as Company1_31_1_,
    company2_.version as version2_31_1_,
    company2_.Address_ID as Address5_31_1_,
    company2_.Company_Code as Company3_31_1_,
    company2_.Company_Name as Company4_31_1_,
    company2_.Phone_ID as Phone6_31_1_,
    phone3_.Phone_ID as Phone1_77_2_,
    phone3_.version as version2_77_2_,
    phone3_.Company_ID as Company5_77_2_,
    phone3_.Fax_Number as Fax3_77_2_,
    phone3_.Phone_Number as Phone4_77_2_,
    company4_.Company_ID as Company1_31_3_,
    company4_.version as version2_31_3_,
    company4_.Address_ID as Address5_31_3_,
    company4_.Company_Code as Company3_31_3_,
    company4_.Company_Name as Company4_31_3_,
    company4_.Phone_ID as Phone6_31_3_ 
from
    testing.Company company0_ 
left outer join
    testing.Address address1_ 
        on company0_.Address_ID=address1_.Address_ID 
left outer join
    testing.Company company2_ 
        on address1_.Company_ID=company2_.Company_ID 
left outer join
    testing.Phone phone3_ 
        on company0_.Phone_ID=phone3_.Phone_ID 
left outer join
    testing.Company company4_ 
        on phone3_.Company_ID=company4_.Company_ID 
where
    company0_.Company_ID=?

1 个答案:

答案 0 :(得分:1)

您的问题是地址实体中的拼写错误:

@JoinColumn(name = "Comapny_ID")

应该是

@JoinColumn(name = "Company_ID")