Spring JPA hibernate postgresql partion and timestamp without timezone - java

After searching a lot, I have a trouble to save timestamp data in PostgreSQL base using Spring JPA Hibernate.
Here my main table in PostgreSQL, it's a partitionning table :
CREATE TABLE public.archive_traffic_measure
(
measure_point_id integer NOT NULL,
measure_agregation_id integer NOT NULL,
measure_datetime timestamp without time zone NOT NULL,
measure_type_id integer NOT NULL,
any_flow integer,
f_any_flow smallint,
hgv_flow integer,
f_hgv_flow smallint,
occupation_rate smallint,
f_occupation_rate smallint,
average_speed smallint,
f_average_speed smallint,
CONSTRAINT pk_archive_traffic_measure PRIMARY KEY (measure_point_id, measure_agregation_id, measure_datetime, measure_type_id)
)
Here my main entity with spring JPA hibernate with #SQLInsert to suppress the check and don't have the problem with hibernate that no row is insert (because the row is insert in a child table) :
#Entity
#Table(name = "archive_traffic_measure")
#SQLInsert(sql = "INSERT INTO archive_traffic_measure (measure_point_id," +
"measure_agregation_id," +
"measure_datetime," +
"measure_type_id," +
"any_flow," +
"f_any_flow," +
"hgv_flow," +
"f_hgv_flow," +
"occupation_rate," +
"f_occupation_rate," +
"average_speed," +
"f_average_speed) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", check = ResultCheckStyle.NONE)
public class ArchiveTrafficMeasure implements Serializable {
#EmbeddedId
private ArchiveTrafficMeasureId id;
#Column(name = "any_flow")
private Integer anyFlow;
#Column(name = "f_any_flow")
private Integer F_AnyFlow;
#Column(name = "hgv_flow")
private Integer hgvFlow;
#Column(name = "f_hgv_flow")
private Integer F_hgvFlow;
#Column(name = "occupation_rate")
private Integer occupationRate;
#Column(name = "f_occupation_rate")
private Integer F_occupationRate;
#Column(name = "average_speed")
private Integer averageSpeed;
#Column(name = "f_average_speed")
private Integer F_averageSpeed;
}
And my id's entity with Spring JPA hibernate :
#Embeddable
#Table(name = "archive_traffic_measure")
public class ArchiveTrafficMeasureId implements Serializable {
#Column(name = "measure_point_id")
private int measurePointId;
#Column(name = "measure_agregation_id")
private int measureAgregationId;
#Column(name = "measure_datetime", columnDefinition = "timestamp without time zone")
#Temporal(TemporalType.TIMESTAMP)
private Date measureDateTime;
#Column(name = "measure_type_id")
private int measureType;
}
When I try to insert new data I have this error :
ERROR: column "measure_datetime" is of type timestamp without time zone but
expression is of type integer
The problem is the request be like :
INSERT INTO archive_traffic_measure VALUES (1,2,2018-04-18 17:00:00+01,1,40,null,null,null,null,null,null,null)
So the quote arount the timestamp is missing...
I have try to put the quote in the #SQLInsert "(?,?,'?',?,?,?,?,?,?,?,?,?)" but with that, I have the error :
The column's index is out of bound : 12, number of column : 11.
I have also try "(?,?,''?'',?,?,?,?,?,?,?,?,?)" with double quote
Or "(?,?,?::timestamp,?,?,?,?,?,?,?,?,?)" but nothing is working for far.
Can someone help me ? Thanks in advance.

First, add nullable = false for 'measure_datetime' column definition also remove composite key from #SQLInsert:
#SQLInsert(sql = "INSERT INTO archive_traffic_measure ("any_flow," +
"f_any_flow," +
"hgv_flow," +
"f_hgv_flow," +
"occupation_rate," +
"f_occupation_rate," +
"average_speed," +
"f_average_speed) VALUES (?,?,?,?,?,?,?,?,?)", check = ResultCheckStyle.NONE)
Then, when you persist ArchiveTrafficMeasure, you should set PK as ArchiveTrafficMeasureId and persist it.

Related

Spring Data and Oracle returns different result from query? Why?

My JPA query is very complex because of that I am using nativequery, But when I run it returns different results from JAVA query and Oracle query. However Oracle query returns correct result.What could be the reason for this ?
I changed SELECT statement, try to fetch all of them by a.* and also try to fetch only necessary column. I also remove DATE comparison statements. However interestingly JPA query returns 4 row and all of them were same exactly. I also try to fetch data without TRUNC keyword in DATE comparison part, and it is not worked. In addition I add schema names in front of tables in native query which is not worked again.
public Optional<LocalDateTime> getPaymentPeriod2(LocalDate reportDate, String loanId)
{
final String reportDateParemeter = reportDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
final String query = "SELECT st.t_no, st.islem_tarihi, st.s_kodu, st.odeme_suresi " +
"FROM k_tarihce st, k_tanim sta, kt_tanim tta " +
"WHERE st.s_kodu = sta.s_kodu " +
"AND tta.s_tipi = sta.s_tipi AND tta.t_kodu = st.t_kodu " +
"AND st.t_no = :loanId AND st.t_kodu in (37,55) " +
"AND st.s_kodu = 'K1' AND (TRUNC(st.odeme_suresi) >= TRUNC(TO_DATE(:reportDateParemeter,'yyyy-mm-dd')))";
final Query loanGetPaymentPeriodQuery = entityManager.createNativeQuery(query, LoanStatusHistory.class);
loanGetPaymentPeriodQuery.setParameter("reportDateParemeter", reportDateParemeter);
loanGetPaymentPeriodQuery.setParameter("loanId", loanId);
List<LoanStatusHistory> loanStatusHistoryList = loanGetPaymentPeriodQuery.getResultList();
return loanStatusHistoryList.isEmpty() ? Optional.empty() : Optional.of(loanStatusHistoryList.get(0).getPaymentPeriod());
}
And my entity is:
#Entity
#Table(name = "K_TARIHCE", schema = "LOS")
#Getter
#Setter
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Cacheable(false)
#IdClass(LoanStatusHistoryId.class)
public class LoanStatusHistory {
#Id
#Column(name = "T_NO")
private String loanId;
#Id
#Column(name = "ISLEM_TARIHI")
private LocalDate processDate;
#Id
#Column(name = "S_KODU")
private String statusCode;
#Column(name = "ODEME_SURESI")
private LocalDateTime paymentPeriod;
#Column(name = "T_KODU")
private String historyCode;
}
class LoanStatusHistoryId implements Serializable {
private String loanId;
private transient LocalDate processDate;
private String statusCode;
}
I want to same result with Oracle SQL developer. My main objective is fetch ODEME_SURESI field.
I found solution, again !interestingly! Here my new code below, I just not mapped my native query with entity, and return some LocalDate.
final Query loanGetPaymentPeriodQuery = entityManager.createNativeQuery(query);
loanGetPaymentPeriodQuery.setParameter("reportDateParemeter", reportDateParemeter);
loanGetPaymentPeriodQuery.setParameter("loanId", loanId);
final List<LocalDate> loanStatusHistoryList = loanGetPaymentPeriodQuery.getResultList();
return loanStatusHistoryList.isEmpty() ? Optional.empty() : Optional.of(loanStatusHistoryList.get(0));
This is the only change I made. I still not know why it worked?.

Native named query fails with exception "column is of type date but expression is of type bytea"when NULL LocalDate is given as input

Query:
INSERT INTO PERSON
(email, mobile, party_id, affiliate_id, eligibility, member_start_date, created_by, created_dt, first_name, last_name, google_connected)
values
('xxx#yyy.org', NULL, 123, '123', '1', NULL, NULL, '2018-8-30 21:45:56.859000 -6:0:0', 'xxx', 'yyy', '0')
ON CONFLICT (email)
DO UPDATE SET create_dt = '2018-8-30 21:45:56.859000 -6:0:0' where email = ?
When the LocalDate value is not null, it works fine. Facing this issue only when LocalDate value is given as null.
Even after PostgreSQL casting, it does the same.
Exception stacktrace:
2018-08-30 21:10:48,372 -- [ERROR]-- There was an unexpected problem
with your request org.postgresql.util.PSQLException: ERROR: column
"member_start_date" is of type date but expression is of type bytea
Hint: You will need to rewrite or cast the expression. Position: 185
at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:645)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:495)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:380) at sun.reflect.GeneratedMethodAccessor98.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy185.executeQuery(Unknown Source) at at
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
... 149 common frames omitted
Entity:
#Entity(name = "person")
#EqualsAndHashCode(callSuper = false)
public class PersonEntity extends Audit {
#Id
#GeneratedValue
#Column(name = "person_id", columnDefinition = "uuid", updatable = false)
private UUID id;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "email")
#NotNull
private String email;
#Column(name = "mobile")
private String mobile;
#Column(name = "party_id")
private Long partyId;
#Column(name = "affiliate_id")
private String affiliateId;
#Column(name = "eligibility")
#NotNull
private Boolean eligibility;
#Column(name = "member_start_date")
private LocalDate memberStartDate;
#Column(name = "google_connected")
private Boolean googleAccountConnected;
}
PostgreSQL table definition; it's missing google_connected column which is not important:
CREATE TABLE person
(
person_id UUID NOT NULL,
email VARCHAR(128) NOT NULL,
mobile VARCHAR(20),
party_id INTEGER,
affiliate_id VARCHAR(20),
eligibility BOOLEAN NOT NULL,
member_start_date DATE,
created_by VARCHAR(128) NOT NULL,
created_dt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_by VARCHAR(128) DEFAULT NULL,
updated_dt TIMESTAMP NULL,
CONSTRAINT person_pk PRIMARY KEY ( person_id )
);
Because the query is native, Hibernate doesn't know the data types which to expect, so when you pass a null it defaults to the generic Serializable type handler. Changing this behaviour breaks compatibility with other databases.
Postgres, however parses the query immediately and determines what types are acceptable, and it always checks for type before it checks for null. They are the only ones who can fix this, but refuse to do so and say it works as intended.
The only solutions for you are:
use JPQL
use managed entities
use hard-coded nulls in the query string where you need to
Fortunately for the third option, with Hibernate you can use named parameters in native queries, so you don't have to do positional calculations for when something is available and when it isn't.
edit: 4th solution that I've discovered since.
You have your query:
Query q = em.createNativeQuery("UPDATE...");
Have some static final LocalDate object somewhere:
public static final LocalDate EPOCH_DATE = LocalDate.of(1970, 1, 1);
then call the query like this:
q.setParameter("start_date", EPOCH_DATE);
q.setParameter("start_date", nullableDateParam);
The first time you call setParameter for a parameter, Hibernate uses the class to resolve the type. The second time you call it, the type is already resolved, so a null will work.
It's an old question, but there is a more useful way:
your query...
.setParameter("transaction_id", null, LongType.INSTANCE)
It works.
Found from https://forum.hibernate.org/viewtopic.php?p=2493645
Going to newer versions of hibernate 5.1.17 and above + postgres seems to have exhibited this behavior. Looking into the code, when it binds a type that has no value, the old hibernate code attempted to resolve the type through a typeresolver. The newer versions of hibernate's implementation state that it will not guess.
public Type resolveParameterBindType(Object bindValue) {
if ( bindValue == null ) {
// we can't guess
return null;
}
We ended up just setting a default value based on the type first, and then the real null value.

JPA: set default value when remove relationship

I have a self joined entity Authority joined by parent id, I modify the entire family tree just by modify the root, creating and modifying a tree node works well, but when I delete a node, jpa set the entity's parent id to null, in consideration of the database performance, I use not null in every columns, as a result, it brings an MySQLIntegrityConstraintViolationException: Column 'parent_id' cannot be null. The SQL is update t_authority set parent_id=null where parent_id=?.
So, my question is, why the default value 0L I set to parentId not works? What should I do to make it use the default value in parentId when removing the relationship.
#Entity
#Table(name = "t_authority")
#SQLDelete(sql =
"UPDATE t_authority " +
"SET status = " + DB_STATUS_NOT_EXISTS +
" WHERE id = ?")
#Where(clause = "status = " + DB_STATUS_EXISTS)
public class Authority implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private long id;
#Column(name = "authority_name")
private String authorityName;
#Column(name = "authority_Id")
private String authorityId;
#Column(name = "parent_id")
private Long parentId = 0L;
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinColumn(name = "parent_id")
#Where(clause = "status = " + DB_STATUS_EXISTS)
private Set<Authority> childAuthorities;
mysql table
create table `t_authority` (
`id` bigint unsigned not null primary key auto_increment,
`authority_name` varchar(100) not null default '' comment 'authority name',
`authority_id` varchar(100) not null default '' comment 'authority id',
`parent_id` bigint unsigned not null default 0 comment 'parent id'
) engine=innodb default charset=utf8 collate=utf8_general_ci;

Database schema not as per hibernate annotations

I am using hibernate to create entity. The attributes I used are as below :
#Id
#SequenceGenerator(name = "customer-id-gen", sequenceName = "CUSTOMERS_SEQ", allocationSize = 1)
#GeneratedValue(generator = "customer-id-gen", strategy = GenerationType.SEQUENCE)
#Column(name = "CUSTOMER_ID", length = 4, nullable = false)
private int customerId;
#Column(name = "CUSTOMER_NAME", length = 40, unique = false, nullable = false)
private String customerName;
#Column(name = "PHONE_NO", unique = true, nullable = true, length = 10)
private Long phoneNo;
However as i can see through logs that table created is as following structure :
create table CUSTOMER_ALL (
CUSTOMER_ID number(10,0) not null,
CUSTOMER_NAME varchar2(40 char) not null,
PHONE_NO number(19,0) unique,
primary key (CUSTOMER_ID)
)
I am not able to figure out how the phone_no attribute is converted into 19 size and customer_id to 10 ?
As per JPA, length only applies to String types.
Type 'int' controlled the storage size of CUSTOMER_ID.
Type 'Long' controlled the storage size of PHONE_NO.
Do you really want a phone number to be a Long? Better a String?

JPA Error : Primary Key was detected to be NULL

I have a problem with JPA querying a MySQL table that has a column of type geometry. It contains polygons having sets of latitude and longitude as the coordinates. While executing the nativequery to select from the table, I am getting the following error
Exception Description: The primary key read from the row [ArrayRecord(
=> POLYGON((102.642944444444 2.9757087270706,102.642944444444 2.79805447470818,....
=> 16.0
=> 325990)] during the execution of the query was detected to be null. Primary keys must not contain null.
However the table has no row with primary key as null. This specific row has a very large polygon with 66 coordinates. Not sure if the problem is because of this.
Following are the table column names and types
geomarea - geometry
riskvalue - double
id - int (Autoincrement, Primary Key)
Following is the code in my EJB to read the table.
Query query = em.createNativeQuery("select astext(geomarea) geomarea,riskvalue,id from earthquakeRisk where Contains(geomarea,GeomFromText('POINT(" + node.getLongitude() + " "+node.getLatitude()+")'))",Earthquakerisk.class);
geomList.addAll(query.getResultList());
And here is how the fields are declared in the entity class
public class Earthquakerisk implements Serializable {
private static final long serialVersionUID = 1L;
#Basic(optional = false)
#NotNull
#Lob
#Column(name = "geomArea")
private byte[] geomArea;
#Column(name = "riskvalue")
private Double riskvalue;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
Any idea how to solve this?
I found a solution to the problem. Adding it here in case someone finds it useful.
Solved it by removing the Earthquakerisk.class from the query, and changing my List to List. So the working code is now as follows.
List<Object[]> geomList = new ArrayList<>();
Query query = em.createNativeQuery("select astext(geomarea) geomarea,riskvalue,id from earthquakeRisk where Contains(geomarea,GeomFromText('POINT(" + node.getLongitude() + " "+node.getLatitude()+")'))");
geomList.addAll(query.getResultList());

Categories

Resources