选择查询在休眠中不起作用

时间:2018-03-09 14:37:31

标签: spring hibernate select hql

我已经尝试了一切,但仍然无法用select来解决我的问题。这个从表Korisnik中选择所有内容的查询效果很好

@Override
    public List<Korisnik> listKorisnikSvi() {
        String selectKorisnik= "FROM Korisnik";
        return sessionFactory.getCurrentSession()
                .createQuery(selectKorisnik, Korisnik.class)
                .getResultList();
    }

但是当我想从这样的表中选择特定的cloumn时,它会给我错误

@Override
public List<Korisnik> listKorisnikDodaj() {
    String selectQuery = "SELECT Ime FROM Korisnik";        
    try {

        return  sessionFactory.getCurrentSession()
                .createQuery(selectQuery, Korisnik.class)                       

                        .getResultList();

}
catch(Exception ex) {
    //ex.printStackTrace();
    return null;
} 

类型例外报告

Message Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 

这是我的代码:

KorisnikDAOImpl class

@Override
public List<Korisnik> listKorisnikDodaj() {
    String selectQuery = "SELECT Ime FROM Korisnik";        
    try {

        return  sessionFactory.getCurrentSession()
                .createQuery(selectQuery, Korisnik.class)                       

                        .getResultList();

}
catch(Exception ex) {
    //ex.printStackTrace();
    return null;
}

KorisnikDAO班

package kpu.edu.rs.kpub.dao;

import java.util.List;

import kpu.edu.rs.kpub.dto.Korisnik;

public interface KorisnikDAO {

    Korisnik get(int id);
    boolean add(Korisnik korisnik);
    boolean update(Korisnik korisnik);
    boolean delete(Korisnik korisnik);
    Korisnik getByIndeks(String Indeks);

    List<Korisnik> listKorisnikSvi();
    List<Korisnik> listKorisnikDodaj();
    List<Korisnik> listKorisnikId(int id);
}

Korisnik班

package kpu.edu.rs.kpub.dto;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class Korisnik implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int IDKorisnik;
    @NotBlank(message="Унесите име")
    private String Ime;
    @NotBlank(message="Унесите презиме")
    private String Prezime;
    @NotBlank(message="Унесите индекс")
    private String brindeksa;
    @NotBlank(message="Унесите шифру")
    private String Sifra;

    private boolean Enabled;
    @NotBlank(message="Унесите улогу")
    private String Uloga;

    private int GodinaUpisa;
    @NotBlank(message="Унесите смер")
    private String Smer;

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "fkBrIndeksa")
    private Collection<Spi> spiLista;

    public int getIDKorisnik() {
        return IDKorisnik;
    }

    public void setIDKorisnik(int iDKorisnik) {
        IDKorisnik = iDKorisnik;
    }

    public String getIme() {
        return Ime;
    }

    public void setIme(String ime) {
        Ime = ime;
    }

    public String getPrezime() {
        return Prezime;
    }

    public void setPrezime(String prezime) {
        Prezime = prezime;
    }

    public String getBrindeksa() {
        return brindeksa;
    }

    public void setBrindeksa(String brindeksa) {
        this.brindeksa = brindeksa;
    }

    public String getSifra() {
        return Sifra;
    }

    public void setSifra(String sifra) {
        Sifra = sifra;
    }

    public boolean isEnabled() {
        return Enabled;
    }

    public void setEnabled(boolean enabled) {
        Enabled = enabled;
    }

    public String getUloga() {
        return Uloga;
    }

    public void setUloga(String uloga) {
        Uloga = uloga;
    }

    public int getGodinaUpisa() {
        return GodinaUpisa;
    }

    public void setGodinaUpisa(int godinaUpisa) {
        GodinaUpisa = godinaUpisa;
    }

    public String getSmer() {
        return Smer;
    }

    public void setSmer(String smer) {
        Smer = smer;
    }

    public Collection<Spi> getSpiLista() {
        return spiLista;
    }

    public void setSpiLista(Collection<Spi> spiLista) {
        this.spiLista = spiLista;
    }



    @Override
    public String toString() {
        return "Korisnik [IDKorisnik=" + IDKorisnik + ", Ime=" + Ime + ", Prezime=" + Prezime + ", brindeksa="
                + brindeksa + ", Sifra=" + Sifra + ", Enabled=" + Enabled + ", Uloga=" + Uloga + ", GodinaUpisa="
                + GodinaUpisa + ", Smer=" + Smer + "]";
    }





}

1 个答案:

答案 0 :(得分:0)

当您只从数据库中选择一个字段时,Hibernate无法创建Korisnik.class。考虑:

  1. 根据您的第一个示例选择所有并创建班级列表,然后只需在需要时选择所需的字段。
  2. Korisnik.class替换String.class,因为您实际上只是从数据库中检索字符串列表(Ime字段)。