-2

I am new on Hibernate and ms sql server.

I have error like this:

Log: DEBUG SQL:109 - select opmtnasaba0_.id_opmt_nasabah as id_opmt_1_48_0_, opmtnasaba0_.alamat as alamat2_48_0_, opmtnasaba0_.flag_transfer as flag_tra3_48_0_, opmtnasaba0_.id_opdd_kategori_nasabah as id_opdd_4_48_0_, opmtnasaba0_.jabatan as jabatan5_48_0_, opmtnasaba0_.keterangan as keterang6_48_0_, opmtnasaba0_.ktp_npwp as ktp_npwp7_48_0_, opmtnasaba0_.nama as nama8_48_0_, opmtnasaba0_.id_opmt_instansi as id_opmt13_48_0_, opmtnasaba0_.penanggung_jawab as penanggu9_48_0_, opmtnasaba0_.tgl_lahir as tgl_lah10_48_0_, opmtnasaba0_.tgl_status as tgl_sta11_48_0_, opmtnasaba0_.usia as usia12_48_0_ from public.opmt_nasabah opmtnasaba0_ where opmtnasaba0_.id_opmt_nasabah=?

Error: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'public'

.

My jdbc.properties

jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.dialect=org.hibernate.dialect.SQLServer2008Dialect
jdbc.databaseurl=jdbc:sqlserver://localhost\\mssql;databaseName=databaseName
jdbc.username=username
jdbc.password=password

My Configuration

@Configuration
@ComponentScan("com.blah")
@EnableTransactionManagement
@PropertySource({"classpath:/jdbc.properties"})
public class ApplicationContextConfig implements TransactionManagementConfigurer {
    @Autowired
    Environment env;

    @Override
    @Bean
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        // TODO Auto-generated method stub
        return txManager();
    }

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.databaseurl"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));

        return dataSource;
    }

    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.dialect", env.getProperty("jdbc.dialect"));
        properties.put("hibernate.current_session_context_class",
                "com.jamkrindo.SyncSuretyBondCabang.config.TransactionAwareSessionContext");
        //      properties.put("hibernate.hbm2ddl.auto", "create-drop");
        return properties;
    }

    @Autowired
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(DataSource dataSource) {
        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.addProperties(getHibernateProperties());
        sessionBuilder.scanPackages("com.jamkrindo");
        return sessionBuilder.buildSessionFactory();
    }

    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(
            SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(
                sessionFactory);
        //transactionManager.setRollbackOnCommitFailure(true);
        return transactionManager;
    }

    @Bean
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(getDataSource());
    }
}

My Service

OpmtNasabah dmn = (OpmtNasabah) dao.get(OpmtNasabah.class, json.getIdOpmtNasabah());

My Dao

public Object get(Class clazz, Number id) {
        // TODO Auto-generated method stub
        return sessionFactory.getCurrentSession().get(clazz, id);
    }

My assumption is because hibernate use keyword public before table_name.

How to resolve this?

Thanks.

user353gre3
  • 2,747
  • 4
  • 24
  • 27
Dewi N
  • 1
  • 1
  • 2
  • Please paste your code. – Paul Lo Dec 30 '14 at 15:50
  • See http://stackoverflow.com/questions/14932136/make-hibernate-backquote-all-table-column-names - that should fix the problem – user2864740 Dec 30 '14 at 15:58
  • I solved the problem, in my domain model, on annotations table @Table(name = "opmt_nasabah", schema = "public"), i just remove schema = "public". – Dewi N Dec 30 '14 at 16:01
  • My domain model generate by hibernate tools, i develop it on my laptop using postgresql, but running it on windows using sql server. – Dewi N Dec 30 '14 at 16:02
  • @DewiN Maybe the dialect is always PgSQL and not correctly changed? That could do it if public is not a reserved PgSQL word. – user2864740 Dec 30 '14 at 16:05

1 Answers1

0

PUBLIC is a SQL Server reserved word and cannot be used as a bare identifier. Have it quoted as a delimited identifier (eg. "public" or [public]) or choose a different, non-reserved, name for the schema.

Microsoft SQL Server uses reserved keywords for defining, manipulating, and accessing databases. Reserved keywords are part of the grammar of the Transact-SQL language that is used by SQL Server to parse and understand Transact-SQL statements and batches.

Although it is syntactically possible to use SQL Server reserved keywords as identifiers and object names in Transact-SQL scripts, you can [use reserved words only when] using delimited identifiers.

To have hibernate quote all identifiers, see Make hibernate backquote all table / column names - however, this "solution" may be missing a more important issue of why public was not on the need-to-quote list for the specific adapter. (Could it be that a wrong dialect is being used?)

All things aside, I would probably use a more focused (and less reserved) name than "public" as the schema name to avoid similar issues with other tooling.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220