Hi in my spring boot postgresql application, when i retrieve all record using DAO it show column does not exists.
ERROR
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42703
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: column merchantit0_.id does not exist
Position: 8
ERROR: org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/customerplus].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/customerplus] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
org.postgresql.util.PSQLException: ERROR: column merchantit0_.id does not exist
Position: 8
Entity Domain
#Entity
#Table(name = "merchant_item_category")
public class MerchantItemCategory{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id", nullable = false, length = 11)
private long id;
#ManyToOne
#JoinColumn(name = "merchant_id", nullable = false)
private Merchant merchant;
// getters and setters
}
DAO
public List<MerchantItemCategory> getAllMerchantItemCategoryByMerchantId(long id) {
Session session=getSession();
List<MerchantItemCategory>itemCategories=session.createQuery("from MerchantItemCategory where merchant.id=:merchantId and isDelete='0' order by categoryName asc")
.setParameter("merchantId", id)
.list();
return itemCategories;
}
I just checked every object and it is correct, But how this error occurring..!
A potential cause of this error is when you haven't defined the hibernate "default schema" property.
I fixed this issue by adding line below to my application.properties:
spring.jpa.properties.hibernate.default_schema=${your-default-schema-name}
Related
I have 2 views
"client_hours_view"
(ClientHoursInfo.java) and
"client_project_hours_view"
(ClientProjectHoursInfo.java)
The first view already existed in the application and :
Retrieves all the clients in the work hours in total
The second view is the new pne and:
Retrieves all the clients with a list of projects for each client and their hours
Anyways, I just want to find a way to retrieve all the clients (first view) with a list of their respective projects (second view), and the best I could figure out is by adding a field list in the ClientHoursInfo.java :
private List clientProjectList which will contain a list of projects for each client.
I have extracted below the code to show only the relation needed against these 2 views
However I am not able to succesfully run it, since it is showing the below error in the console.
Unknown column 'clientproj0_.clienthours_id'
I would like to get guidance to solve this or find a different approach to meet the requirement.
ClientHoursInfo.java
#Entity
#Table(name = "client_hours_view")
public class ClientHoursInfo {
#Id
#Column(name = "unique_id")
private String uniqueId;
private Long id;
private String client;
#Column(name = "week_ending")
private String weekEnding;
#Column(name = "total_hours")
private Double totalHours;
#OneToMany(mappedBy = "clienthours")
private List<ClientProjectHoursInfo> clientProjectList;
}
ClientProjectHoursInfo.java
#Entity
#Table(name = "client_project_hours_view")
public class ClientProjectHoursInfo {
#ManyToOne(fetch = FetchType.LAZY)
private Employee clienthours;
#Id
#Column(name = "id")
private String Id;
}
FinanceManagerReports.java
clientHours = clientHoursInfoViewRepository.findByWeekEndingBetween(from, to);
for (ClientHoursInfo clientHoursInfo : clientHours) {
clientProjectHours = clientProjectHoursInfoViewRepository
.findByClientIdAndWeekEnding(clientHoursInfo.getId(), clientHoursInfo.getWeekEnding());
clientHoursInfo.setClientProjectList(clientProjectHours);
}
Error Console
[2019-10-02 17:21:57.887] boot - 9656 ERROR [http-nio-8080-exec-3] --- SqlExceptionHelper: Unknown column 'clientproj0_.clienthours_id' in 'field list'
[2019-10-02 17:21:57.888] boot - 9656 ERROR [http-nio-8080-exec-3] --- [dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'clientproj0_.clienthours_id' in 'field list'
I have a ReST API developed with Spring Boot that exposes resources. Using Spring Data JPA CrudRepository
One of this this resources contains a collection of another resource. I'd like to delete a child directly from its repository.
I have tried to use CascadeType = Remove but it still doesn't work.
Parent object :
#Entity
public class Subject{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String icon;
private String image;
#OneToMany( targetEntity=Ability.class, orphanRemoval = true, cascade = CascadeType.REMOVE)
private List<Ability> abilities;
}
Child object :
#Entity
public class Ability {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String color;
private String image;
#OneToMany(targetEntity=Technology.class, orphanRemoval = true, cascade = CascadeType.REMOVE)
private List<Technology> technologies;
But when I tried to delete an ability that is bound to a subject using :
abailityRepository.deleteById(id);
I get following error message :
2019-08-04T13:34:27.987258+00:00 app[web.1]: 2019-08-04 13:34:27.986 WARN 4 --- [io-40382-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23503
2019-08-04T13:34:27.987393+00:00 app[web.1]: 2019-08-04 13:34:27.987 ERROR 4 --- [io-40382-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: update or delete on table "ability" violates foreign key constraint "fkb019eiy6xjwsyxuukgmr9iid6" on table "subject_abilities"
2019-08-04T13:34:27.987397+00:00 app[web.1]: Detail: Key (id)=(100) is still referenced from table "subject_abilities".
2019-08-04T13:34:27.987588+00:00 app[web.1]: 2019-08-04 13:34:27.987 INFO 4 --- [io-40382-exec-5] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2019-08-04T13:34:27.987877+00:00 app[web.1]: 2019-08-04 13:34:27.987 ERROR 4 --- [io-40382-exec-5] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2019-08-04T13:34:27.993333+00:00 app[web.1]: 2019-08-04 13:34:27.992 ERROR 4 --- [io-40382-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [fkb019eiy6xjwsyxuukgmr9iid6]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
2019-08-04T13:34:27.993338+00:00 app[web.1]: org.postgresql.util.PSQLException: ERROR: update or delete on table "ability" violates foreign key constraint "fkb019eiy6xjwsyxuukgmr9iid6" on table "subject_abilities"
2019-08-04T13:34:27.993340+00:00 app[web.1]: Detail: Key (id)=(100) is still referenced from table "subject_abilities".
2019-08-04T13:34:27.993342+00:00 app[web.1]: at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440) ~[postgresql-42.2.5.jar!/:42.2.5]
.......
I'm new using JPA and JPQL for a project, but I got SQL syntax error and I couldn't find the cause.
I'm using spring boot, MySQL.
I got a vote repository
public interface VoteRepository extends JpaRepository<Vote, Long> {
....
#Query(value = "SELECT NEW com.self.polls.model.ChoiceVoteCount(v.choice.id, count(v.id)) FROM Vote v WHERE v.poll.id = :pollId GROUP BY v.choice.id", nativeQuery = true)
List<ChoiceVoteCount> countByPollIdGroupByChoiceId(#Param("pollId") Long pollId);
....
}
ChoiceVoteCount
public class ChoiceVoteCount {
private Long choiceId;
private Long voteCount;
}
Vote
public class Vote {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "poll_id", nullable = false)
private Poll poll;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "choice_id", nullable = false)
private Choice choice;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "user_id", nullable = false)
private User user;
}
But I got error when running this query
2019-03-27 16:01:34.783 ERROR 68064 --- [nio-5000-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : 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 '.self.polls.model.ChoiceVoteCount(v.choice.id, count(v.id)) FROM Vote v WHERE v.' at line 1
2019-03-27 16:01:34.787 ERROR 68064 --- [nio-5000-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
java.sql.SQLSyntaxErrorException: 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 '.self.polls.model.ChoiceVoteCount(v.choice.id, count(v.id)) FROM Vote v WHERE v.' at line 1
You have to add a constructor in ChoiceVoteCount.
To play safe, the default constructor could also be added.
public ChoiceVoteCount() {
}
public ChoiceVoteCount(Long choiceId, Long voteCount) {
this.choiceId = choiceId;
this.voteCount = voteCount;
}
i have web-jpa project, my project work from postgres and mysql database, but when I add new data in mysql db i get exception:
HTTP Status 500 - Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not read a hi value - you need to populate the table: hibernate_sequence; nested exception is org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence
ERROR [http-nio-8080-exec-5] <unknown>.<unknown> could not read a hi value - you need to populate the table: hibernate_sequence
Stacktrace
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not read a hi value - you need to populate the table: hibernate_sequence; nested exception is org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence
...
root cause
org.springframework.orm.jpa.JpaSystemException: could not read a hi value - you need to populate the table: hibernate_sequence; nested exception is org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence
...
root cause
org.hibernate.id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_sequence
...
This is my entities class:
User
#Entity
#Table(name = "USERS_TABLE")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "USERS_NAME")
private String name;
#Column(name = "USERS_EMAIL")
private String email;
}
UserKey
#Entity
#Table(name = "USERS_KEY")
public class UserKey {
#Id
#GeneratedValue
private Long id;
#Column(name = "KEY_COLUMN")
private int key;
#OneToOne
#JoinColumn(name = "USERS_ID")
private User user;
}
Following are my two related tables
public class VProduct{
#Id
#Column(name="product_id")
private Long productId;
#OneToMany(mappedBy="vProduct", fetch = FetchType.EAGER, cascade={CascadeType.REMOVE})
private Set<ProductPan> pans;
}
public class ProductPan {
#Id
#Column(name="product_pan_id")
private Long productPanId;
#Fetch(FetchMode.JOIN)
#ManyToOne(optional=true, fetch=FetchType.EAGER)
#JoinColumn(name="product_id", referencedColumnName = "product_id", insertable = false, updatable = false)
private VProduct vProduct;
}
I get the following exception when i try to query the VProduct tabel.
org.hibernate.exception.SQLGrammarException: ERROR: column pans0_.product_pan_id does not exist
What am i doing wrong here ? At the moment ProductPan table is empty. And its not guranteed to have any items for a VProduct. if thats the problem is there any annotation i can add to ignore that null data problem ?
This is the hibernate query and result
14:39:49,994 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select pans0_.product_id as product3_101_1_, pans0_.product_pan_id as product1_1_, pans0_.product_pan_id as product1_71_0_, pans0_.pan_id as pan2_71_0_, pans0_.product_id as product3_71_0_ from product_pan pans0_ where pans0_.product_id=?
14:39:50,113 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) SQL Error: 0, SQLState: 42703
14:39:50,116 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) ERROR: column pans0_.product_pan_id does not exist