Using Database in Production Postgres - Not Accepted Using Table with Camelcase

Asked

Viewed 72 times

0

Good morning to all.

I’m trying to communicate with a Postgress database and so far so good. Inside the bank I have a table called Clientelegado. But every time the application goes up a legacy cliente_table is created.

I’ve tried using @Table(name="Clientelegacy"). I have tried without @Table and use the class name just like the table, but Hibernate insists on create this legacy cliente_table. As I will only consume the data of this table and I can not take the risk of making changes in the database, I am commenting on the line below:

 @GeneratedValue(strategy = GenerationType.IDENTITY)

As I am developing a Rest Api, the return I have is from the legacy cliente_table and not from the Clientelegacy table.

This could be some problem in the application.properties ?

Configuration of application.properties

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:postgresql://localhost:5431/comunicacao
spring.datasource.username=postgres
spring.datasource.password=056409

#spring.jpa.hibernate.naming.implicit-   strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.jpa.show-sql=true
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
#spring.jpa.generate-ddl=true
#spring.jpa.hibernate.ddl-auto=validate

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.flyway.baselineOnMigrate = true
#server.port=8083

Class Configuration

@Entity
@Table(name="ClienteLegado")
public class Cliente implements Serializable {

private static final long serialVersionUID = 1L;
 @Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "ClienteLegadoId")
private Long id;

2 answers

2

Spring JPA by default uses underscore to split names into Camel case. Change the naming Strategy on application.properties

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.EJB3NamingStrategy

Reference

  • Changing the naming Strategy now it keeps creating the table like this : cliente_legacy where the table name in the bank is Clientelegacy. In my view it should not create any table. Have any more idea what this problem might be ?

  • What version of Spring are you using?

  • Postgresql uses two types of identifiers, the common identifier that does not differentiate between upper and lower case and the delimited identifier (or quoted Identifier), written in quotation marks and that must follow exactly as written in its definition. Thus name, NAME and Name refer to the same identifier but "identifier", "Identifier" or "IDENTIFIER" are different. Check how you, or your application, made the setting in your database.

0


Guys, solution found was.

In the application.properties settings

spring.jpa.hibernate.naming.implicit- strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Mapping table name

 @Entity
 @Table(name="\"ClienteLegado\"")

Mapping name of fields

 @Column(name="\"Codigo\"")
 private String codigo;

 @Column(name="\"Nome\"")
 private String nome;
 Gets e Sets

It is worth remembering that this case is a case that escapes the rule, because the table in the customer’s database includes the name in case Camel , such as Clientelegado. Making tests when the table name is in lower case which is the default of Postgres, there is no need for this all by following the traditional method. To understand how the classes were assembled it was necessary to reverse engineer the Eclipse where I assembled the entities from the database. Eclipse set it up and followed suit and it all worked out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.