/ / Zapytanie wyboru nie działa w trybie hibernacji - wiosna, hibernacja, wybór, hql

Wybór zapytania nie działa w trybie hibernacji - wiosna, hibernacja, wybór, hql

Próbowałem wszystkiego, ale nadal nie mogę rozwiązać mojego problemu z select.To zapytanie o wybór wszystkich z tabeli Korisnik działa świetnie

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

ale gdy chcę wybrać konkretny cloumn z takiej tabeli, daje mi to błąd

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

Wpisz raport wyjątków

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)

Oto mój kod:

Klasa KorisnikDAOImpl

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

Klasa 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);
}

Klasa 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 + "]";
}





}

Odpowiedzi:

0 dla odpowiedzi № 1

Hibernate nie może utworzyć pliku Korisnik.class gdy wybierasz tylko jedno pole z bazy danych. Rozważać:

  1. Wybieranie wszystkich i tworzenie listy klas zgodnie z pierwszym przykładem, a następnie wybranie pola, które jest potrzebne, gdy jest potrzebne.
  2. Wymiana Korisnik.class z String.class ponieważ faktycznie pobierasz tylko listę Strings (pole Ime) z bazy danych.