错误:未知栏' this_.idHardDrives'在'字段列表'

时间:2015-02-13 21:13:52

标签: java hibernate hibernate-annotations

请帮助ERROR:未知栏' this_.idHardDrives'在'字段列表'。 无法理解该领域的idHardDrives'存在于数据库中。

HardDrives.java

package main.java.table;


import javax.persistence.*;

@Entity
@Table(name = "hardDrives")
public class HardDrives {

@Id @GeneratedValue
@Column(name = "idHardDrives")
private int idHardDrives;
@Column(name = "name")
private String nameHardDrives;
@Column(name = "capacity")
private int capacity;
@Column(name = "interface")
private String interFace;
@Column(name = "connectionType")
private String connectionType;
@ManyToOne
@JoinColumn (name = "idBrand")
private Brands brands;




public int getIdHardDrives() {
    return idHardDrives;
}

public void setIdHardDrives(int idHardDrives) {
    this.idHardDrives = idHardDrives;
}

public String getNameHardDrives() {
    return nameHardDrives;
}

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

public int getCapacity() {
    return capacity;
}

public void setCapacity(int capacity) {
    this.capacity = capacity;
}

public String getInterFace() {
    return interFace;
}

public void setInterFace(String interFace) {
    this.interFace = interFace;
}

public String getConnectionType() {
        return connectionType;
}

public void setConnectionType(String connectionType) {
        this.connectionType = connectionType;
}

public void setBrand(Brands brands){
    this.brands = brands;
}

public Brands getBrands(){
    return brands;
    }
}

Brands.java         包main.java.table;

import java.util.List;
import java.util.Set;

import javax.persistence.*;

@Entity
@Table(name = "Brands")
public class Brands {
@Id @GeneratedValue
@Column(name = "idBrand")
private int idBrand;
@Column(name = "nameBrand")
private String nameBrand;
@OneToMany (mappedBy="brands")
private List <HardDrives> hardDrivesList;

public Brands(){}
public Brands(int idbr, String namebr){
    this.idBrand = idbr;
    this.nameBrand = namebr;
}

public int getIdBrand(){
    return idBrand;
}

public void setIdBrand(int id){
    this.idBrand = id;
}

public String getNameBrand(){
    return nameBrand;
}

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


public List<HardDrives> getHardDrives(){
    return hardDrivesList;
}

    public void setHardDrives(List<HardDrives> hardDrives){
        this.hardDrivesList = hardDrives;
    }

}

HardDrivesDaoImpl.java

package main.java.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Session;

import main.java.dao.HardDrivesDao;
import main.java.table.Brands;
import main.java.table.HardDrives;
import main.java.util.HibernateUtil;

public class HardDrivesDaoImpl implements HardDrivesDao {

    @Override
    public void addHardDrive(HardDrives hardDrive) throws SQLException {
        Session session = null;
        try{
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(hardDrive);
        session.getTransaction().commit();
    } catch(Exception e){
        e.printStackTrace();
    } finally {
        if ((session != null) && (session.isOpen()))
        session.close();
    }


}

@Override
public void deleteHardDrive(HardDrives hardDrive) throws SQLException {
    Session session = null;
    try{
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(hardDrive);
        session.getTransaction().commit();
    } catch(Exception e){
        e.printStackTrace();
    } finally {
        if ((session != null) && (session.isOpen()))
        session.close();
    }

}

@Override
public HardDrives getHardDrive(int id) throws SQLException {
    HardDrives result = null;
    Session session = null;
    try{
        session = HibernateUtil.getSessionFactory().openSession();
        result = (HardDrives)session.load(HardDrives.class, id);
    } catch(Exception e){
        e.printStackTrace();
    } finally {
        if ((session != null) && (session.isOpen()))
        session.close();
    }
    return result;
}

@Override
public List<HardDrives> getHardDrives() throws SQLException {
    List <HardDrives> hardDrives = null;

    Session session = null;
    try{
        session = HibernateUtil.getSessionFactory().openSession();
        hardDrives = session.createCriteria(HardDrives.class).list();
        return null;
        } catch(Exception e){
            e.printStackTrace();
        } finally {
            if ((session != null) && (session.isOpen()))
            session.close();
        }
            return hardDrives;
    }

}

Main.java

package main.java.general;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import main.java.dao.impl.HardDrivesDaoImpl;
import main.java.table.Brands;
import main.java.table.HardDrives;
import main.java.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class Main {


public static void main(String[] args) throws SQLException {

    HardDrivesDaoImpl hardDrives = new HardDrivesDaoImpl();
    List<HardDrives>hardDrivesList = hardDrives.getHardDrives();
    for(HardDrives hd :hardDrivesList){
        System.out.print("Id: " + hd.getIdHardDrives()); 
        System.out.print("Name: " + hd.getNameHardDrives()); 
        System.out.println("Capacity: " + hd.getCapacity());
        System.out.println("Interface: " + hd.getInterFace());
        System.out.println("Connection Type: " + hd.getConnectionType());
        System.out.println("Brand: " + hd.getBrands());

        System.out.println("\n");
    }

    }
}

错误

фев 13, 2015 4:57:58 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
фев 13, 2015 4:57:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.7.Final}
фев 13, 2015 4:57:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
фев 13, 2015 4:57:58 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
фев 13, 2015 4:57:58 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
фев 13, 2015 4:57:58 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
фев 13, 2015 4:57:58 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
фев 13, 2015 4:57:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
фев 13, 2015 4:57:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/storage]
фев 13, 2015 4:57:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
фев 13, 2015 4:57:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
фев 13, 2015 4:57:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
фев 13, 2015 4:57:59 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
фев 13, 2015 4:57:59 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
фев 13, 2015 4:57:59 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
фев 13, 2015 4:57:59 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
фев 13, 2015 4:57:59 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
фев 13, 2015 4:57:59 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'this_.idHardDrives' in 'field list'
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
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.extract(ResultSetReturnImpl.java:91)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
at org.hibernate.loader.Loader.doList(Loader.java:2554)
at org.hibernate.loader.Loader.doList(Loader.java:2540)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
at org.hibernate.loader.Loader.list(Loader.java:2365)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
at main.java.dao.impl.HardDrivesDaoImpl.getHardDrives(HardDrivesDaoImpl.java:73)
at main.java.general.Main.main(Main.java:44)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'this_.idHardDrives' in 'field list'
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:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
... 14 more
Exception in thread "main" java.lang.NullPointerException
at main.java.general.Main.main(Main.java:45)

1 个答案:

答案 0 :(得分:4)

类和表中的名称字段存在问题。 MySQL Workbench没有显示该字段名称中的空格,我纠正了它并且一切正常。