# Fix HHH-12106 Database name not quoted at schema update on MS-SQLServer not working

**URL:** <https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914>\
**Category:** Hibernate ORM\
**Created:** [June 12, 2018, 6:47am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914 "2018-06-12T06:47:23Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 12, 2018, 6:47am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/1 "2018-06-12T06:47:23Z")

</div>

Hello,  
Last year I posted a problem in old forum and created HHH-12106 which should habe been fixed in hibernate 5.3.1-final according to the versions’ change log. The problem was that a database name containing a hyphen in Microsoft SQL-Server is not quoted at hibernate schema update. I tested it today with version 5.3.1-final and the problem still occurs:

```auto
Hibernate: alter table serie-m.paul.MOMS_SPLIT_DOC add TYPE_STRING varchar(32)
[org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table serie-m.paul.MOMS_SPLIT_DOC add TYPE_STRING varchar(32)" via JDBC Statement]
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Falsche Syntax in der Nähe von '-'.

```

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [June 12, 2018, 8:11am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/2 "2018-06-12T08:11:42Z")

</div>

We have the [`SqlServerQuoteSchemaTest`](https://github.com/hibernate/hibernate-orm/blob/574378dc6fddf4753c720f303696fa82bc8abc15/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/.java) that works just fine.

---

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 12, 2018, 9:47am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/3 "2018-06-12T09:47:30Z")

</div>

I see in test property hibernate.globally\_quoted\_identifiers=true is set. Do you confirm that this property has to be used in general (javadoc: Should all database identifiers be quoted) just because one identifier (in this case the database name) should be quoted?  
I used it also, but it still does not work. The schema and table name is now wrapped into brackets, but the database name (serie-m in this case) not:

```auto
Hibernate: alter table serie-m.[paul].[MOMS_SPLIT_DOC] add [TYPE_STRING] varchar(32)
[org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table serie-m.[paul].[MOMS_SPLIT_DOC] add [TYPE_STRING] varchar(32)" via JDBC Statement]

```

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [June 12, 2018, 9:50am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/4 "2018-06-12T09:50:57Z")

</div>

Try to supply a Pull Request which replicates your issue.

---

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 12, 2018, 12:09pm UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/5 "2018-06-12T12:09:25Z")

</div>

I can reproduce it with SqlServerQuoteSchemaTest when using MS-SQL-Server 2008 and appropriate Dialect org.hibernate.dialect.SQLServer2008Dialect which is very old of course. Then the update DDL contains the unquoted database name:

```auto
    alter table serie-m.[my-schema].[my_entity] 
       add [title] varchar(255);

```

With SQL-Server 2012 and appropriate Dialect version the DDL contains no database name. This might be caused by JDBC DatabaseMetadata which probably does not supply a catalog resp. database name (I did not debug so deep). But in principal the code is not prepared to quote database name though property hibernate.globally\_quoted\_identifiers=true and it is also not tested by SqlServerQuoteSchemaTest DDL comparison string at Line 154, where only schema and entity name is quoted.

---

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 12, 2018, 12:36pm UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/7 "2018-06-12T12:36:04Z")

</div>

In the DDL to CREATE the table the catalog name is not used in despite to the update DDL:

```auto
create table [paul].[my_entity] (
       [id] int not null,
        primary key ([id])
    )

alter table seriem.[paul].[my_entity] 
       add [title] varchar(255)

```

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [June 12, 2018, 4:34pm UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/8 "2018-06-12T16:34:04Z")

</div>

But why do you need the database name in your qualified name?

The database can be implied from the JDBC URL string:

```
jdbc:sqlserver://localhost;instance=SQLEXPRESS;databaseName=hibernate_orm_test
```

---

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 13, 2018, 6:34am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/9 "2018-06-13T06:34:58Z")

</div>

Thanks for the hint. But after I added the database name to the JDBC URL it still appears in the DDL:  
my URL:  
`hibernate.connection.url=jdbc:sqlserver://mssql-2008-std.hq.kwsoft.de:1433;instance=MSSQL-2008-STD;databaseName=serie-m`  
DDL statement:

```auto
alter table serie-m.[paul].[my_entity] 
   add [title] varchar(255)

```

I don’t need the database name in the qualified name, it is added by Hibernate for some reason to the alter table DDL statements’ table name.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [June 13, 2018, 8:14pm UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/10 "2018-06-13T20:14:30Z")

</div>

Sends us a Pull Request which replicates the issue based on the ones we have committed previously. We will investigate it afterwards.

---

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 14, 2018, 6:12am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/11 "2018-06-14T06:12:08Z")

</div>

Puh, a lot of work for me for what you already have resp. could adapt easily. Simply run SqlServerQuoteSchemaTest against your MS-SQL-Server and you should see the non-quoted database name in the full qualified entity name in the “alter table add columns” DDL statement.  
Still want me to send a pull request ?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [June 14, 2018, 6:24am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/12 "2018-06-14T06:24:18Z")

</div>

Any change to the codebase must have an associated test case. We need the test case to see the impact on other databases as well.

---

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 14, 2018, 6:28am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/13 "2018-06-14T06:28:03Z")

</div>

The already existing test case is MS-SQL-specific, according to its name SqlServerQuoteSchemaTest. Can I adapt it or do you want me to create an new one ?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [June 14, 2018, 6:39am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/14 "2018-06-14T06:39:29Z")

</div>

Just adapt the existing one, replicate the issue and send a Pull Request. Thanks.

---

<div class="post-metadata">

**Author:** ![grischapaul](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@grischapaul](https://discourse.hibernate.org/u/grischapaul)\
**Post date:** [June 14, 2018, 6:43am UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/15 "2018-06-14T06:43:28Z")

</div>

Ok, I will try. Thank you!

---

<div class="post-metadata">

**Author:** ![gsmet](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gsmet/32/9_2.png) [@gsmet](https://discourse.hibernate.org/u/gsmet)\
**Post date:** [August 29, 2018, 12:51pm UTC](https://discourse.hibernate.org/t/fix-hhh-12106-database-name-not-quoted-at-schema-update-on-ms-sqlserver-not-working/914/16 "2018-08-29T12:51:29Z")

</div>

Hi @grischapaul ,

Once you have a modified test case, please create an associated new JIRA so that we can track this issue.

From what you say, it looks like we are doing something wrong indeed but I checked the code and we are supposed to quote the catalog name (which is the database name in this case).

I don’t have SQL Server handy to check that but could you add a breakpoint in `org.hibernate.mapping.Table#sqlAlterStrings()` and check what it is doing. As you can see, we have some code to quote everything so there’s something fishy here.

Thanks!
