getting
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name
'partnerIdPartner'.
application.properties:
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Table creation:
CREATE TABLE [partnersystem] (
[idPartnerSystem] INT IDENTITY(1,1) ,
[partner_idPartner] INT NOT NULL DEFAULT NULL ,
[Name] NVARCHAR(45) NULL DEFAULT NULL ,
[Domain] NVARCHAR(45) NULL DEFAULT NULL ,
[Code] NVARCHAR(45) NULL DEFAULT NULL ,
[PartnerSystem_idSystem] INT NOT NULL DEFAULT NULL ,
[UpdateUser] NVARCHAR(45) NULL DEFAULT NULL ,
[UpdateDT] DATETIME NULL DEFAULT NULL ,
CONSTRAINT [partnersystem_PRIMARY] PRIMARY KEY CLUSTERED ([idPartnerSystem]), CONSTRAINT [partnersystem_fk_PartnerSystem_partner] FOREIGN KEY ("partner_idPartner") REFERENCES "partner" ( "idPartner" ) ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT [partnersystem_fk_PartnerSystem_System] FOREIGN KEY ("PartnerSystem_idSystem") REFERENCES "system" ( "idSystem" ) ON UPDATE NO ACTION ON DELETE NO ACTION);
CREATE INDEX [partnersystem_fk_PartnerSystem_partner] ON [partnersystem] ([partner_idPartner]);
CREATE INDEX [partnersystem_fk_PartnerSystem_System] ON [partnersystem] ([PartnerSystem_idSystem]);
JPA Entity:
#Entity
#Table(name = "partnersystem")
public class PartnerSystem {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "idPartnerSystem")
private int idPartnerSystem;
#Column(name = "partner_idPartner" )
private int partnerIdPartner;
#Column(name = "Name")
private String name;
#Column(name = "Domain" )
private String domain;
#Column(name = "Code" )
private String code;
#Column(name = "PartnerSystem_idSystem" )
private int partnerSystemIdSystem;
#Column(name = "UpdateUser" )
private String updateUser;
my repository:
#Repository
public interface PartnerSystemRepository extends JpaRepository<PartnerSystem,
Integer>{
public PartnerSystem findByPartnerIdPartner(int partnerIdPartner);
}
executing simple query throws an error.
public List<Object[]> findAllPartnerSystem(int id) {
String test =
"SELECT idPartnerSystem, partnerIdPartner, name, domain, code, partnerSystemId" +
" FROM PartnerSystem " +
"WHERE partnerIdPartner = ?"
;
Query query = em.createNativeQuery(test);
query.setParameter(1, id);
List<Object[]> results = query.getResultList();
for (Object[] row : results) {
}
return results;
}
In native queries you have to use the column name not the property name:
"SELECT idPartnerSystem, partner_idPartner, name, domain, code, PartnerSystem_idSystem" +
" FROM partnersystem " +
"WHERE partner_idPartner=
But I suggest using JPQL queries and not native queries.
Related
I am trying to persist an object using the entity manager in a springboot application.
but I am getting the below exception
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'interval, last_updated) values ('5MIN', '2023-02-01 00:56:16')' at line 1
Query is : insert into cms_kpi_config (interval, last_updated) values ('5MIN', '2023-02-01 00:56:16.0')
at org.mariadb.jdbc.internal.util.ExceptionMapper.get(ExceptionMapper.java:125)
at org.mariadb.jdbc.internal.util.ExceptionMapper.throwException(ExceptionMapper.java:69)
at org.mariadb.jdbc.MariaDbStatement.executeQueryEpilog(MariaDbStatement.java:242)
at org.mariadb.jdbc.MariaDbClientPreparedStatement.executeInternal(MariaDbClientPreparedStatement.java:210)
at org.mariadb.jdbc.MariaDbClientPreparedStatement.executeUpdate(MariaDbClientPreparedStatement.java:186)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:384)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 223 more
Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'interval, last_updated) values ('5MIN', '2023-02-01 00:56:16')' at line 1
Query is : insert into cms_kpi_config (interval, last_updated) values ('5MIN', '2023-02-01 00:56:16.0')
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.getResult(AbstractQueryProtocol.java:939)
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQueries(AbstractQueryProtocol.java:775)
at org.mariadb.jdbc.MariaDbClientPreparedStatement.executeInternal(MariaDbClientPreparedStatement.java:201)
... 226 more
I don't seem to understand what the problem is.
here is my table definition
CREATE TABLE IF NOT EXISTS `cms_kpi_config` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`interval` enum('5MIN','30MIN','60MIN') NOT NULL DEFAULT '5MIN',
`last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
here is my Entity
#Entity
#Table(name = "cms_kpi_config")
#JsonInclude(JsonInclude.Include.NON_NULL)
public class CmsKPIConfig {
private Integer id;
private String interval;
private Date lastUpdated;
#Id
#Column(name = "id", nullable = false, unique = true)
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Column(name = "interval", nullable = false)
#NotNull
public String getInterval() {
return interval;
}
public void setInterval(String interval) {
this.interval = interval;
}
#Column(name = "last_updated")
#JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public Date getLastUpdated() {
return lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
}
In my Dao service class, I'm using entitymanager as below.
private EntityManager manager;
this.manager.persist(obj);
Please help
DB Schema (H2 database):
create table book (
id_book bigint auto_increment not null primary key,
title varchar(255) not null,
id_author bigint not null,
id_genre bigint not null
);
create table comment (
id_comment bigint auto_increment not null primary key,
id_book bigint not null,
comment_text varchar(255) not null
);
Domain classes:
public class Book {
#Id
#Column("id_book")
private Long id;
private String title;
#Column("id_author")
AggregateReference<Author, Long> author;
#Column("id_genre")
AggregateReference<Genre, Long> genre;
#MappedCollection(idColumn = "id_book", keyColumn = "id_comment")
List<Comment> comments = new ArrayList<>();
public void addComment(String commentText) {
comments.add(new Comment(commentText));
}
//getters and setters
}
public class Comment {
#Column("id_comment")
private Long id;
#Column("comment_text")
private String text;
public Comment(String text) {
this.text = text;
}
public Comment() {
}
//getters and setters
}
I have the problem when I add a comment to the book.
```java
#Override
#Transactional
public String addComment(long bookId, String commentText) {
var book = bookRepository.findById(bookId);
return book.map(b -> {
b.addComment(commentText);
b = bookRepository.save(b);
return bookConverter.convertToString(b);
}).orElse("Book not found!");
}
It generates SQL like this...
Executing SQL batch update [INSERT INTO "COMMENT" ("comment_text", "id_book", "id_comment") VALUES (?, ?, ?)]
... adds values for id_comment field like 0, 1, 2, 3 and these values intersect with existing ones. So I get Primary Key Violation. Why it adds id_comment field to the INSERT expression?
Why it adds id_comment field to the INSERT expression?
Because you told it to.
The following annotation tells Spring Data JDBC to store the index of the list in the id_comment column.
#MappedCollection(idColumn = "id_book", keyColumn = "id_comment")
Your data model is missing a column for the list index. Add that column and use it as keyColumn in the #MappedCollection annotation
Hi below is my schema definition
CREATE TABLE LOANS (
LOAN_ID NUMBER(9,0) PRIMARY KEY,
CORR_ID VARCHAR(5) NULL
);
CREATE TABLE DV_LOAN_PARTICIPANTS (
LOAN_ID NUMBER(9,0) ,
DVP_PARTICIPANT_NAME VARCHAR(50) NOT NULL,
DVP_PARTICIPANT_TYPE VARCHAR(50) NOT NULL,
PRIMARY KEY ("LOAN_ID", "DVP_PARTICIPANT_NAME")
);
LOANS Entity
#Table(name = "LOANS")
#Entity
public class Loans {
#Id
#Column(name = "LOAN_ID")
private Long loanId;
#OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinColumn(name = "LOAN_ID")
#MapKey(name = "dvpParticipantName")
private Map<String, DVLoanParticipants> dvLoanParticipantsMap;
// getter and setters
}
DV_LOAN_PARTICIPANTS Entity
#Table(name = "DV_LOAN_PARTICIPANTS")
#Entity
public class DVLoanParticipants implements Serializable {
#Id
#Column(name = "LOAN_ID")
private Long loanId;
#Id
#Column(name = "DVP_PARTICIPANT_NAME")
private String dvpParticipantName;
#Column(name = "DVP_PARTICIPANT_TYPE")
private String dvpParticipantType;
// getters and setters
}
Service Class is
DVLoanParticipants dvLoanParticipants = new DVLoanParticipants();
dvLoanParticipants.setLoanId(Long.valueOf("196801758"));
dvLoanParticipants.setDvpParticipantName("VKP");
dvLoanParticipants.setDvpParticipantType("Developer");
Loans loanInsert = new Loans();
loanInsert.setLoanId(Long.valueOf("196801758"));
Map<String,DVLoanParticipants> partyMap = new HashMap<>();
partyMap.put("VKP",dvLoanParticipants);
loanInsert.setDvLoanParticipantsMap(partyMap);
repository.save(loanInsert);
But when i am executing the save i am getting error as
NULL not allowed for column "LOAN_ID"; SQL statement:
insert into dv_loan_participants (dvp_participant_type, loan_id, dvp_participant_name) values (?, ?,
?)
Git Hub Code
https://github.com/vinoykp/spring-jpa/tree/master/spring-boot-hibernate-crud-demo
I had the similar question
Why Value is not getting assigned in JPA for insert statement
What is the issue in association?
I have to query on timestamp field for current date and get only 1 record.
I can write the query like:
#Query("Select i From Log i Where i.createdBy = :userId And DATE(i.createdDate) = CURRENT_DATE")
JPA Entity:
#Entity
#Table(name = "log")
public class Log {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "log_id")
private Long logId;
#Column(name = "address")
private String address;
#Column(name = "created_date", updatable = false)
#Temporal(TemporalType.TIMESTAMP)
#DateTimeFormat(style = "yyyy-MM-dd HH:mm:ss")
private Calendar createdDate;
//SETTERS AND GETTERS
}
TABLE:
CREATE TABLE `log` (
`log_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`address` VARCHAR(30) NOT NULL,
`created_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`log_id`)
)
ENGINE=InnoDB
;
here i am not able to limit the records,
I know we can limit the records by sending Pageable, but again i have to get the record from list.
Is there any way to do this ?
how can we do it in spring data jpa method name ?
You can use a Spring Data repository to do this like the following:
public interface LogRepository extends JpaRepository<Log, Long> {
// just get one or null
Log findOneByCreatedByAndCreatedDate(Instant createdBy, Instant createdDate);
// get all, but pagable
List<Log> findAllByCreatedByAndCreatedDate(Instant createdBy, Instant createdDate, Pageable pageable);
}
I assume you use Instant as timestamp, but this should also work for the other Java 8 date types or the old Date class.
Within your business logic you can now call:
Log log = logRepository.findOneByCreatedByAndCreatedDate(YOUR_TIMESTAMP, Instant.now());
// or
Log allLogs = logRepository.findOneByCreatedByAndCreatedDate(YOUR_TIMESTAMP, Instant.now(), PageRequest.of(0, 50));
I have the following tables
CREATE TABLE APPUSERS (
APPUSERS_ID INT IDENTITY(1,1),
USERNAME VARCHAR(254) NOT NULL,
PASSWORD VARCHAR(100) NOT NULL,
PRIMARY KEY (USERNAME)
);
CREATE TABLE ALL_ROLES (
ROLE_ID INT IDENTITY(1,1),
ROLENAME VARCHAR(100) NOT NULL,
PRIMARY KEY (ROLENAME)
);
CREATE TABLE USER_ROLES(
USER_ROLE_ID INT IDENTITY(1,1),
USERNAME VARCHAR(254) NOT NULL,
CONSTRAINT FK_USERNAME FOREIGN KEY (USERNAME)
REFERENCES APPUSERS (USERNAME),
ROLENAME VARCHAR(100) NOT NULL,
CONSTRAINT FK_ROLENAME FOREIGN KEY (ROLENAME)
REFERENCES ALL_ROLES (ROLENAME),
PRIMARY KEY (username,rolename)
)
I have created the corresponding Entities(See below) and Repositories
#Entity
#Table(name = "appusers")
public class User {
private Long id;
private String username;
private String password;
private String passwordConfirm;
private Set<Role> roles;
#Id
#Column(name="APPUSERS_ID")
#GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
#Transient
public String getPasswordConfirm() {
return passwordConfirm;
}
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}
#ManyToMany
#JoinTable(name = "USER_ROLES", joinColumns = #JoinColumn(name = "USERNAME"), inverseJoinColumns = #JoinColumn(name = "ROLENAME"))
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
And
#Entity
#Table(name = "USER_ROLES")
public class Role {
private Long id;
#Column(name="USERNAME")
private String name;
private Set<User> users;
#Id
#Column(name="USER_ROLE_ID")
#GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#ManyToMany(mappedBy = "roles")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
When I start the application I get the following error
Foreign key (FKrs04la1w0u7vtog85q1hxlse9:user_roles [rolename])) must have same number of columns as the referenced primary key (user_roles [username,rolename])
I am not able to figure what what is the issue here. Any help is greatly appreciated.
I think the table mappings are all correct but not sure why this error is occurring.
There are a couple of issues on your code, let me explain for steps:
Relationship many to many, you need to create an intermediate table in order to do that so you need to fix these following aspects:
User entity
#Id
#Column(name = "APPUSERS_ID")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToMany(mappedBy = "users")
private Set<Role> roles;
Role entity
#Id
#Column(name = "USER_ROLE_ID")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToMany
#JoinTable(name = "role_user",
joinColumns = { #JoinColumn(name = "role_id") },
inverseJoinColumns = { #JoinColumn(name = "user_id") })
private Set<User> users;
If you want these entities are generated on database by JPA hibernate just put the following property configuration(you just need to create database with name).
spring.jpa.hibernate.ddl-auto=update
Else I leave you here scripts to execute on database.
-- Table: public.appusers
-- DROP TABLE public.appusers;
CREATE TABLE public.appusers
(
appusers_id bigint NOT NULL,
password character varying(255) COLLATE pg_catalog."default",
password_confirm character varying(255) COLLATE pg_catalog."default",
username character varying(255) COLLATE pg_catalog."default",
CONSTRAINT appusers_pkey PRIMARY KEY (appusers_id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.appusers
OWNER to postgres;
-- Table: public.role_user
-- DROP TABLE public.role_user;
CREATE TABLE public.role_user
(
role_id bigint NOT NULL,
user_id bigint NOT NULL,
CONSTRAINT role_user_pkey PRIMARY KEY (role_id, user_id),
CONSTRAINT fkma2afyyxc0mraogwivmj0klfe FOREIGN KEY (role_id)
REFERENCES public.user_roles (user_role_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fkmhbomge36ygro6rth9negs1ye FOREIGN KEY (user_id)
REFERENCES public.appusers (appusers_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.role_user
OWNER to postgres;
-- Table: public.user_roles
-- DROP TABLE public.user_roles;
CREATE TABLE public.user_roles
(
user_role_id bigint NOT NULL,
username character varying(255) COLLATE pg_catalog."default",
CONSTRAINT user_roles_pkey PRIMARY KEY (user_role_id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.user_roles
OWNER to postgres;