here's the problem:
I want to withdraw a list of UserDomains from database with a method:
#Query("SELECT dom FROM User usr JOIN usr.domains dom")
List<UserDomain> findDomains();
here is my entity and embeddable class:
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "usr_id")
private Integer id;
#Column(name = "usr_username")
private String username;
#Column(name = "usr_password")
private String password;
#Column(name = "usr_email")
private String email;
#ElementCollection
#JoinTable(name = "user_domains", joinColumns = #JoinColumn(name = "udo_usr_id"))
private Set<UserDomain> domains;
#Embeddable
public class UserDomain {
#Column(name = "udo_dom_guid")
private String domGuid;
#Column(name = "udo_company_guid")
private String companyGuid;
the problem is when I'm trying to execute the query within GET method I'm getting an error:
"could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
I've been trying to solve this for many hours :( thanks in advance
Related
New to Hibernate and HQL in general. I have these following entities.
Employee entity
public class Employee implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column
private Integer authority;
#Column
private String username;
#Column(name = "user_password")
private String password;
#Column
private String title;
#OneToOne
#JoinColumn(name = "person_id", referencedColumnName = "id")
private Person person;
#Column
private Integer gender;
#Column
private String ssn;
#Column(name = "car_info")
private String carInfo;
#Column(name = "birth_date")
private Date birthDate; //java.util Date vs java.sql.Date?
#OneToOne
#JoinColumn(name = "visa_status_id", referencedColumnName = "id")
private VisaStatus visaStatus;
#Column(name = "license_number")
private Integer licenseNumber;
#Column(name = "license_expiration_date")
private Date licenseExpirationDate;
#ManyToOne
#JoinColumn(name = "housing_id", referencedColumnName = "id")
private House house;
}
Person entity
public class Person implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "middle_name")
private String middleName;
#Column(name = "preferred_name")
private String preferredName;
#ManyToOne
#JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
#Column
private String email;
#Column(name = "ceil_phone")
private String ceilPhone;
#Column(name = "work_phone")
private String workPhone;
}
House entity
public class House implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#OneToOne
#JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
#ManyToOne
#JoinColumn(name = "contact_id")
private Contact contactHR;
#ManyToOne
#JoinColumn(name = "landlord_id")
private Person landlord;
}
I'm trying to run this HQL:
select person from Employee where house.id=:houseId
I'm basically trying to get all the Person with the houseId from the Employee. Since I'm trying to only get the person, I'm using the select cause.
But I'm getting these errors:
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'house.id'
org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'house.id' [select person from example.project.domain.entity.Employee where house.id=:houseId]
I tried the HQL without the select clause and it worked:
from Employee where house.id=:houseId
But I only need person so I didn't want to get everything. Any idea what's wrong?
You asked
Hibernate Invalid Path Exception using HQL
The problem lies on the written query
select person from Employee where house.id=:houseId
Which should contain proper referencing
select p.person from Employee p where p.house.id=:houseId
Why ?
The FROM clause defines which entities the data is going to be selected. Hibernate, or any other JPA implementation, maps the entities to the according database tables and the syntax of a JPQL FROM clause is similar to SQL and indeed uses the entity model for referencing database attributes, which in root query execution (same thing you are doing) one must use the same referencing strategy, which in your case is to use aliases, as A.Panfilov said in comments regardless of hibernate, even in aliasing in SQL is a good practice.
You may find more in here.
I have three tables: ScanMeta, Scan and Batch.
#Entity
#Table(name = "scan_meta_letter")
public class ScanMetaLetter {
#Id
#Column(name = "scan_meta_id")
private Long id;
#OneToOne(mappedBy = "scanMeta")
#JoinColumn(name = "scan_id")
private Scan scan;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
....
}
#Entity
#Table(name = "scan")
public class Scan {
#Id
#GeneratedValue
#Column(name = "scan_id")
private Long id;
#Column(name = "read_date")
private OffsetDateTime readDate;
#Column(name = "deletion_date")
private OffsetDateTime deletionDate;
#Column(name = "type", length = 10)
#Enumerated(EnumType.STRING)
private ScanType type;
#ManyToOne(fetch = FetchType.LAZY)
private Batch batch;
....
}
#Entity
#Table(name = "batch")
public class Batch {
#Id
#GeneratedValue
#Column(name = "id")
private Long id;
#Column(name = "batch_status")
#Enumerated(EnumType.STRING)
private BatchStatus batchStatus;
#Column(name = "batch_type", length = 10)
#Enumerated(EnumType.STRING)
private BatchType batchType;
....
}
When I create Specification<ScanMettaLetter>, I fetch tables: Scan and Batch.
Specification<ScanMetaLetter> s = Specification.where((root, criteriaQuery, criteriaBuilder) -> {
criteriaQuery.distinct(true);
root.fetch("scan", JoinType.LEFT).fetch("batch", JoinType.LEFT);
return null;
});
If I would like the response to sort by scan.type - everything is ok. But if sort by - scan.batch.batchStatus, I get the error:
could not prepare statement; SQL [select distinct scanmetale0_.scan_meta_id as scan_met1_7_0_,
scan1_.scan_id as scan_id1_3_1_, batch2_.id as id1_0_2_, scanmetale0_.first_name as
recipien3_9_0_, scanmetale0_.last_name as recipien5_9_0_, scan1_.batch_id as
batch_i13_3_1_, scan1_.deletion_date as deletion3_3_1_, scan1_.read_date as read_dat8_3_1_,
scan1_.type as type11_3_1_, batch2_.batch_status as batch_st3_0_2_,
batch2_.batch_type as batch_ty4_0_2_ from
scan_meta_letter scanmetale0_ left outer join scan scan1_ on
scanmetale0_.scan_meta_id=scan1_.scan_id left outer join batch batch2_ on
scan1_.batch_id=batch2_.id cross join batch batch6_ where scan1_.batch_id=batch6_.id and
(scan1_.reg_code like ?) and scan1_.type=? and batch6_.batch_status=? and 1=1 order by
batch6_.batch_status asc limit ?]; nested exception is
org.hibernate.exception.SQLGrammarException: could not prepare statement
I see that two alias for batch is created -> batch2 and batch6. Why? Can I bypass this somehow?
How can I store an object (entity) in memory to get it later only in a working application with Hibernate?
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private long id;
#Column(name = "name")
private String name;
#Column(name = "password")
private String password;
//without save this to database but in memory
private boolean drunk;
}
How to get an object from memory with variable "drunk"?
I'm using now this to get object:
session.createQuery("SELECT u FROM User u WHERE u.name LIKE :name")
.setParameter("name", name)
.getResultList();
You can user #Transient annotation, It indicates that a field is not to be persisted in the database.
import javax.persistence.Transient;
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private long id;
#Column(name = "name")
private String name;
#Column(name = "password")
private String password;
//without save this to database but in memory
#Transient
private boolean drunk;
}
However you would need to define how this value would be set after you read it again from database later.
I am creating web application in Spring andI was using local MySql database and everything was good.
But I decided to use online free database.
when i am trying to execute any JpaRepository query, I got error like this:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '?3_4_0_, employee0_.permission as permissi4_4_0_,
employee0_.ulica as ulica5' at line 1
#Entity
#Data
#Builder
#Table(name = "`Pracownicy`")
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "`id`")
private int id;
#Column(name = "`Imię`")
private String name;
#Column(name = "`Nazwisko`")
private String surname;
#Column(name = "`Ulica`")
private String street;
#Column(name = "`Miasto`")
private String town;
#Column(name = "`Kod_pocztowy`")
private String zipCode;
#Column(name = "`Permission`")
private String permission;
#Column(name = "`id_aktywnego_zamówienia`")
private Integer activeOrder;
#OneToOne(cascade = {CascadeType.ALL}, mappedBy = "employee")
private EmployeeLogin employeeLogin;
#OneToMany(cascade = {CascadeType.ALL}, mappedBy = "employee")
#LazyCollection(LazyCollectionOption.FALSE)
private List<Order> orders = new ArrayList<>();
}
Any ideas why?
These are my propertise.
spring.datasource.url=jdbc:mysql://sql2.freesqldatabase.com:3306/sql2270378?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username= *********
spring.datasource.password= **********
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
I have this entity
#Entity
#Table(name = "REPORT_TASCK")
#Data
public class ReportTasck {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;
#Column(name = "type")
public String type;
#Column(name = "status")
public Integer status;
#Column(name = "type")
#OneToMany(mappedBy = "reportTasck", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
public List<Bill> bills;
}
and
#Entity
#Table(name = "BIIL")
#Data
public class Bill {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;
#Column(name = "neme")
public String neme;
#Column(name = "status")
public Integer status;
#ManyToOne()
#JoinColumn(name = "REPORT_TASCK_ID")
public ReportTasck reportTasck;
}
then I tried fill it and save
ReportTasck reportTasck = new ReportTasck();
reportTasck.setStatus(0);
reportTasck.setType("standart");
List<Bill> bills = new ArrayList<>();
for (BillDto billDto : all) {//640 items
Bill bill = new Bill();
bill.setStatus(0);
bill.setNeme(billDto.getBill());
bill.setReportTasck(reportTasck);
bills.add(bill);
}
reportTasck.setBills(bills);
reportTasckRepository.save(reportTasck);
but when start this line reportTasck.setBills(bills); I get error in debbug mode Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate entity.ReportTasck.toString()
and when I tried save I get this error Method threw 'org.springframework.dao.DataIntegrityViolationException' exception.
could not execute statement; SQL [n/a]; constraint [null]
I dont understand why I get this error and how fix