0

I have a spring boot project that runs with this setup:

@SpringBootApplication(exclude=SecurityAutoConfiguration.class)
@ImportResource("classpath:/application-context.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

I discovered hibernate was mapping my atributes without converting camelCase to SNAKE_CASE, instead, it was just applying upper_case to it (since Oracle is case insensitive, it doesn't matter):

Caused by: java.sql.SQLException: ORA-00904: "PESSOA0_"."IDPESSOA": invalid identifier

Here's my entity:

@Entity
@Table(name = "BC_PESSOA")
@NamedQuery(name = "Pessoa.findAll", query = "SELECT p FROM Pessoa p")
public class Pessoa implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  private Long idPessoa;

I wanted hibernate to implicitly map idPessoa attribute to ID_PESSOA column, as it's default behavior. I don't know why this isn't happening.

I have tons of other entities with 30-40 attributes (I know it's bad practice, but I'm not allowed to change it into smaller objects) and I wanted to leave the objects mapping responsibility completely to hibernate. I know I could use Eclipse's JPA facet to create mapped entities with database but I'm really curious to understand why the standard behavior isn't standard anymore.

At application-context.xml, I have this piece of code showing some beans configuration(covered private names):

enter image description here

I used xirulei as a dummy configuration to see any errors, but none appeared, only the same behavior as before. Changing my dialect although actually broke spring, if it matters.

EDIT

I discovered my project is using spring boot 1.4.5.RELEASE but due other non debatable reasons Hibernate is 4.2.19.FINAL. Looking here I saw this piece:

before adding mappings:

SessionFactory sf = new Configuration()         
.setNamingStrategyDelegator(ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE)
      .addFile("Item.hbm.xml")
      .addFile("Bid.hbm.xml")
      .buildSessionFactory();

Knowing I'm using application-context.xml to configure spring/jpa/hibernate, where does 'before adding mappings' should be? Should I use SpringNamingStrategy (as old spring) or hibernate.ejb.naming_strategy? And besides that, which values would be valid?

rado
  • 5,720
  • 5
  • 29
  • 51
  • Why are you using an xml to configure the `LocalContainerEntityManagerFactoryBean`? Ditching that would make Spring Boot apply its own defaults (you are basically disabling that with this). – M. Deinum Aug 17 '18 at 17:35
  • I didn't asked the people yet behind this decision, I'm new at my company and I'm trying to use `NamingStategy` with the minimum possible changes at xml – rado Aug 17 '18 at 18:03

1 Answers1

1

Snake case is the default for Spring Boot, while what you are seeing is the default for Hibernate. See Hibernate field naming issue with Spring Boot (naming strategy).

Since you explicitly configure your EntityManagerFactory with properties, I guess this makes Boot skip its default configuration for it. Since your naming strategies don't exist Hibernate falls back to its default, which is what you are seeing.

In order to fix this you can either reference Spring Boots ImprovedNamingStrategy explicit, or configure JPA via Spring Boots application.properties and not provide a naming strategy.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Jens, I updated my question specifying what I want. Thanks for your reply and if you want/can, update yours too. – rado Aug 17 '18 at 17:32