it-swarm.com.de

konnte ResultSet nicht extrahieren

public class DBUtilU {
    private static Session scss;
    private static SessionFactory sfactory;
    static {
        Configuration cfg = new Configuration();
        cfg.addAnnotatedClass(Userdetails.class);
        cfg.addAnnotatedClass(Code.class);
        cfg.addAnnotatedClass(Messages.class);
        cfg.addAnnotatedClass(Comments.class);
        cfg.configure();
        new SchemaExport(cfg);
        sfactory = cfg.buildSessionFactory();
    }
}

public static List<Code> searchCodeTags(String tags) {
    List<Code> codelist = new ArrayList<Code>();
    try {
        scss = sfactory.getCurrentSession();
        scss.beginTransaction();
        String query = "from Code where Tags=" + tags;
        Query queryResult = scss.createQuery(query);
        for(Object c : queryResult.list())
            codelist.add((Code)c);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return codelist;
}

package UsersProperty;
@Entity
public class Code {
    @Id
    @GeneratedValue
    private int codeId;
    private int Rating;
    private String fileName;
    private String OwnerName;
    private String PrjName;
    private String Tags;
    @ManyToOne()
    @JoinColumn(name="userid")
    private Userdetails userdetails;
    @OneToMany(targetEntity=Comments.class, mappedBy="targetCode")
    private List<Comments> cm;

    public int getRating() {
        return Rating;
    }
    public void setRating(int rating) {
        Rating = rating;
    }

    public List<Comments> getCm() {
        return cm;
    }
    public void setCm(List<Comments> cm) {
        this.cm = cm;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getOwnerName() {
        return OwnerName;
    }
    public void setOwnerName(String ownerName) {
        OwnerName = ownerName;
    }

    public String getPrjName() {
        return PrjName;
    }
    public void setPrjName(String prjName) {
        PrjName = prjName;
    }

    public String getTags() {
        return Tags;
    }
    public void setTags(String tags) {
        Tags = tags;
    }
    public Userdetails getUserdetails() {
        return userdetails;
    }
    public void setUserdetails(Userdetails userdetails) {
        this.userdetails = userdetails;
    }

    public Code(String fileName, String ownerName, String prjName,String tAgs) {
        super();
        this.fileName = fileName;
        OwnerName = ownerName;
        PrjName = prjName;
        Tags=tAgs;
    }
    public Code() {
        super();
    }

    public static List<Code> CodeSearch(String TAGS){
        return DBUtilU.searchCodeTags(TAGS);
    }
}

Konsole:

> Hibernate: select code0_.codeId as codeId1_0_, code0_.OwnerName as OwnerNam2_0_, code0_.PrjName as PrjName3_0_, code0_.Rating as Rating4_0_, code0_.Tags as Tags5_0_, code0_.fileName as fileName6_0_, code0_.userid as userid7_0_ from Code code0_ where code0_.Tags=Overloading
Jun 17, 2013 8:38:05 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42703
Jun 17, 2013 8:38:05 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: column "overloading" does not exist
  Position: 252
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
  at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.Java:123)
  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.doList(Loader.Java:2522)
  at org.hibernate.loader.Loader.doList(Loader.Java:2508)
  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.Java:2338)
  at org.hibernate.loader.Loader.list(Loader.Java:2333)
  at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.Java:490)
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.Java:355)
  at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.Java:195)
  at org.hibernate.internal.SessionImpl.list(SessionImpl.Java:1269)
  at org.hibernate.internal.QueryImpl.list(QueryImpl.Java:101)
  at DB.DBUtilU.searchCodeTags(DBUtilU.Java:117)
  at UsersProperty.Code.CodeSearch(Code.Java:90)
  at Test.Main.main(Main.Java:34)
Caused by: org.postgresql.util.PSQLException: ERROR: column "overloading" does not exist
  Position: 252
  at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.Java:2157)
  at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.Java:1886)
  at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.Java:255)
  at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.Java:555)
  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.Java:417)
  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.Java:302)
  at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.Java:56)
  ... 17 more

Problem:

Dies geschieht, wenn ich die searchCodeTags-Funktion von der DBUtilU-Klasse aus ausführen. Was passiert hier?

7
A.R.H

Ändern Sie Ihre Anfrage wie folgt:

String query = "from Code where Tags='" + tags+"'";

Ansonsten wie folgt:

String hql = "from Code where Tags=:tags";
Query query = session.createQuery(hql);
query.setParameter("tags",tags);

Der Vergleich in der where -Klausel erfolgt mit einem Literal und nicht mit einer anderen Spalte. Es muss also entweder wie im ersten Fall zitiert werden oder wie im zweiten Fall eine Bind-Variable verwendet werden.

12
Mubin

Sehen Sie sich Ihre Fehlermeldung an (ich gebe zu, dass der Stacktrace lang ist, im Allgemeinen ist der interessante Teil oben).

ERROR: ERROR: Spalte "Überladung" ist nicht vorhanden

from Code code0_ wobei code0_.Tags = Überladung

Sie müssen 'Overloading' angeben, da es sich nicht um einen Spaltennamen handelt, sondern um einen Literalwert. Noch besser, setzen Sie es als Bindungsvariable.

4
Thilo

wenn Sie über camelCase verfügen, wird JPA in Camel_Case konvertiert. Die Konvertierung in Kleinbuchstaben funktioniert problemlos.

0
vaquar khan