0

I have spring 4+ hibernate 4.3 + psql 9.5 application and faced some problems with camelcase field transalations.

Hibernate: select trainservi0_.trainServiceId as trainSer1_16_, trainservi0_.category as category2_16_, trainservi0_.date as date3_16_, trainservi0_.description as descript4_16_, trainservi0_.name as name5_16_, trainservi0_.status as status6_16_, trainservi0_.trainNumber as trainNum7_16_, trainservi0_.version as version8_16_ from tms.public.train_service trainservi0_
2016-02-29 13:09:34 WARN  SqlExceptionHelper:144 - SQL Error: 0, SQLState: 42703
2016-02-29 13:09:34 ERROR SqlExceptionHelper:146 - ERROR: column trainservi0_.trainserviceid does not exist
 Perhaps you meant to reference the column "trainservi0_.trainServiceId".

Here is my config:

public class DataBaseProdConfig {

    @Resource
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(env.getRequiredProperty("db.entity.package"));
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        em.setJpaProperties(getHibernateProperties());

        return em;
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(env.getRequiredProperty("db.url"));
        ds.setDriverClassName(env.getRequiredProperty("db.driver"));
        ds.setUsername(env.getRequiredProperty("db.username"));
        ds.setPassword(env.getRequiredProperty("db.password"));

        ds.setInitialSize(Integer.valueOf(env.getRequiredProperty("db.initialSize")));
        ds.setMinIdle(Integer.valueOf(env.getRequiredProperty("db.minIdle")));
        ds.setMaxIdle(Integer.valueOf(env.getRequiredProperty("db.maxIdle")));
        ds.setTimeBetweenEvictionRunsMillis(Long.valueOf(env.getRequiredProperty("db.timeBetweenEvictionRunsMillis")));
        ds.setMinEvictableIdleTimeMillis(Long.valueOf(env.getRequiredProperty("db.minEvictableIdleTimeMillis")));
        ds.setTestOnBorrow(Boolean.valueOf(env.getRequiredProperty("db.testOnBorrow")));
        ds.setValidationQuery(env.getRequiredProperty("db.validationQuery"));
        return ds;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager manager = new JpaTransactionManager();
        manager.setEntityManagerFactory(entityManagerFactory().getObject());

        return manager;
    }

    public Properties getHibernateProperties() {
        try {
            Properties properties = new Properties();
            InputStream is = getClass().getClassLoader().getResourceAsStream("hibernate.properties");
            properties.load(is);

            return properties;
        } catch (IOException e) {
            throw new IllegalArgumentException("Can't find 'hibernate.properties' in classpath!", e);
        }
    }

Here is my entity:

@Entity
@Table(name = "train_service", schema = "public", catalog = "tms")
public class TrainServiceEntity {
    private Long trainServiceId;
    @Id
    @SequenceGenerator(name="train_service_trainServiceId_seq",
            sequenceName="train_service_trainServiceId_seq",
            allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
            generator="train_service_trainServiceId_seq")
    @Column(name = "trainServiceId")
    public Long getTrainServiceId() {
        return trainServiceId;
    }

    public void setTrainServiceId(Long trainServiceId) {
        this.trainServiceId = trainServiceId;
    }
....
}

So Hibernate translates 'trainServiceId' to 'trainserviceid' , but in logs I see the right field name:

Hibernate: select trainservi0_.trainServiceId as trainSer1_16_, ...

What I need to do , that force Hibernate dont translate field names to lowecase?

MeetJoeBlack
  • 2,804
  • 9
  • 40
  • 66
  • You need to set column names of entity in Upper case if you need it in upper case. – Sai prateek Feb 29 '16 at 10:32
  • I dont want the uppercase i wont use the same camelcase as in databse – MeetJoeBlack Feb 29 '16 at 10:42
  • _"So Hibernate translates 'trainServiceId' to 'trainserviceid' , but in logs I see the right field name"_ I don't get you, do you have an issue or not? How do you know that wrong statements are sent to the db? From the db logs? – Dragan Bozanovic Feb 29 '16 at 11:07
  • So when I try to exeucte findAll() statement I see error in logs, because I set 'hibernate.show_sql = true'. Pls see the logs in head of my question – MeetJoeBlack Feb 29 '16 at 11:11

1 Answers1

4

You can try to put the mixedcase columne names in hyphens.

@Column("\"trainServiceId\"")

see persistence tables ,search for 'Tables with special characters and mixed case'