New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Naming issue with native queries [DATAJPA-1225] #1546
Comments
Robert Hunt commented Surely you just need to use the standard backslash escaping that you would use in a regular String? There is nothing special going on here, it's just a String value that you're passing to the @Query(value = "select i.invoice_id as \"invoiceId\", i.booking_tag a... |
Jens Schauder commented Rubert Hunt should be correct. Thomas, can you please confirm? |
Thomas Lang commented Sorry, i cannot confirm. Maybe the error images attached can give you guys some hints. |
Thomas Lang commented By the way, here is my projection interface: /**
* Projection interface for spring data jpa.
* takes the resultset of a spring data jpa query.
*
* @author tlang
*/
public interface InvoiceProjection {
/**
* The invoice id.
*
* @return long
*/
long getInvoiceId();
/**
* The booking tag.
*
* @return String
*/
String getBookingTag();
/**
* The invoice number.
*
* @return String
*/
String getInvoiceNumber();
/**
* The invoice date.
*
* @return LocalDate
*/
LocalDate getInvoiceDate();
/**
* The invoice recipient.
*
* @return String
*/
String getRecipient();
/**
* The invoice state
*
* @return InvoiceState
*/
InvoiceState getInvoiceState();
/**
* The created by property.
*
* @return String
*/
String getCreatedBy();
/**
* The modified by property.
*
* @return String
*/
String getModifiedBy();
} |
Jens Schauder commented So from the screen shots I deduce:
I see 3 potential points of failure here: a) the JDBC driver doesn't return the correct capitalization I just verified that the JDBC driver of Postgres does return alias names correctly capitalized. (Version org.postgres:postgres:42.0.0) |
Thomas Lang commented Dear Jens, thank you for your help. Please note that Oliver Gierke adviced me to do some debugging in the closed #980 here: Maybe this helps finding the issue. Thank you! |
Jens Schauder commented If I interpret your screenshot correctly we are getting the wrong capitalization from Hibernate. Then this is a Hibernate or a Postgres issue. Could you please check if your driver returns correct capitalized column names? If they contain upper case letters file an issue against Hibernate. Please leave a link to the issue so we can track what is going on on the other side |
Thomas Lang commented Dear Jens, i have just uploaded a screenshot of what you suggested. So i will file an issue against Hibernate. Thank you for your help. |
Thomas Lang commented Hello there, i have just filed a hibernate issue: By the way: Again - Well done guys and girls! Kind regards from a fan! |
Thomas Lang commented Hey there, it´s me again. Can you guys confirm that? Kind regards! |
Jens Schauder commented Not a Boot person, but it does look like the mentioned strategies are used. You should be able to override them (see https://stackoverflow.com/questions/41918622/change-hibernate-5-naming-strategy-spring-boot-anootation) and provide a physical naming strategy derived from Or you can just register an appropriate bean. https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/howto-data-access.html#howto-configure-hibernate-naming-strategy Let us know if that works. In the meantime, I'll try to find out why that naming strategy exists in this form |
Thomas Lang commented Hey there Jens, thank you for your help. I will let you know if that works. By the way: @Id
@GeneratedValue
@NotNull
private long invoiceId; to a database (postgres) column named: invoice_id Generally i don´t care how the or mapper or any other tool will name my columns in the datasource. I have other Spring Data JPA Projects which are not Spring Boot based (but "the old" Spring way). I will let you know, if the suggestion from you respectively the hibernate guy will work. Just another question, maybe a little off topic here, but anyway :) Kind regards from a fan. |
Jens Schauder commented About the underscore: I guess what one would expect/like depends on your upbringing. Without underscores, the names get really confusing, especially if you have long property names. regarding "the old way": For one Spring Boot does register these naming strategies so yes it changes the behavior from vanilla Hibernate. Regarding .NET: There is a Spring .Net which seems to be dead (and I don't know who maintained it). Spring Data for .NET seems not realistic at the moment. Since one would need a Spring Data Commons for .Net which would need a Spring Framework for .NET |
Thomas Lang commented Hey there Jens, thank you for your reply. Settings: spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl Repository: Query(value = "select i.invoiceid as invoiceId, i.bookingtag as bookingTag, i.invoicenumber as invoiceNumber, i.createdby as createdBy, i.invoiceapigroups as apiGroups, i.invoicedate as invoiceDate, r.recipient as recipient from invoice i inner join recipient r on (i.recipient_recipientid = r.recipientid) where i.invoicestate = ?2 and i.invoiceapigroups similar to ?1 order by i.invoicedate desc", nativeQuery = true)
Iterable<InvoiceProjection> findAllProjectedByInvoiceStateAndSimilarUserOrApiGroup(String similarToPattern, int state); Hibernate logs: 2017-12-07 13:55:55.478 DEBUG 6784 --- [lTaskExecutor-2] org.hibernate.SQL : select i.invoiceid as invoiceId, i.bookingtag as bookingTag, i.invoicenumber as invoiceNumber, i.createdby as createdBy, i.invoiceapigroups as apiGroups, i.invoicedate as invoiceDate, r.recipient as recipient from invoice i inner join recipient r on (i.recipient_recipientid = r.recipientid) where i.invoicestate = ? and i.invoiceapigroups similar to ? order by i.invoicedate desc But the materialization is still wrong. Please note: Did i miss something? |
Thomas Lang commented Dear Jens, i´ve quickly tailored a test repository. You may use this to locate the error. Please let me know if there are cloning issues. |
Thomas Lang commented ... it´s me again ... I did some debugging into spring data jpa´s internals as Oliver Gierke advised me to do some weeks ago: This is the result from the non native query projection - the naming is right here: This is the result from the native query projection - the naming is wrong here: Maybe this helps. |
Jens Schauder commented Hi Thomas, Also since it just performs a select it doesn't tell us anything about the resulting column names. I tried to reproduce the problem with a separate project but failed to reproduce the issue. Please provide a complete example, which includes the setup (i.e. creates the necessary tables and inserts data as necessary) |
Thomas Lang commented Hello Jens, thanks again for your help. I have updated the repository. I use a maven project which itself uses the spring starter platform. All dependencies will be pulled from that. If you look at the effective pom (Eclipse or Intellij) you can see all Dependencies used. I don´t know how to provide other projects, since i´m just familiar with maven and spring starter. I have switched the Further all you need for the test project to run is a postgres (9.4 or higher) database engine to connect to (preferably on localhost to set a simple user/password combination). When you run the test the command runner creates one invoice and then selects it. The select fails, because of the naming issue. Thank you! By the way. |
Jens Schauder commented Based on you repostiories I created two new ones which in my opinion demonstrate:
Repository for reproducing this issue: https://github.com/schauder/issue-DATAJPA-1225 Repository for reproducing the Hibernate issue: https://github.com/schauder/issue-HHH-12119 I'll update the hibernate issue. |
Thomas Lang commented Hey there Jens, thank you for your help. Good job. |
Jens Schauder commented Closing, since everybody seems to agree now that this is a Hibernate issue |
Thomas Lang opened DATAJPA-1225 and commented
Hello there, my problem is generally described by this issue which has already been closed.
Please see the comments!
https://jira.spring.io/browse/DATAJPA-980
Issue:
The naming gets confused when doing a nativequery:
Please note:
from the following query should map to the corresponding
property from the projection interface.
But postgres ignores the naming and names it invoiceid, which does not have a matching property.
To use the right naming scheme (because i use a projection interface which is already existing - see above) i have to use double quotes in postgres on the column aliasing as suggested by Jens Schauder in the comments (the closed issue) and here: https://stackoverflow.com/questions/20878932/are-postgresql-column-names-case-sensitive
So how do i use/escape double quotes within the data jpq query?
The query should be the following, but in that way the compiler does not accept this quoting
Thank you all for your outstanding work on data jpa and your help!
Kind regards from a fan :)
Thomas
Affects: 1.11.8 (Ingalls SR8)
Reference URL: https://jira.spring.io/browse/DATAJPA-980
Attachments:
The text was updated successfully, but these errors were encountered: