2

I've recently added synonyms to our DB. These synonyms hold the entire entity information, hence have their own mapping file. Connecting to the synonyms is not an issue. The issue is when using the hbm2ddl tool.

If I include the synonym mapping files, the hbm2ddl tool creates the synonym table as a normal table, it should be ignored. I'm not sure how to flag the entity mapping as a synonym (or if possible) and restrain hibernate from creating a new table for this synonym.

If I exclude the mapping files, then the tool does not pass validation since other files refer to the synonym entities. Example:

   <many-to-one name="creator" class="com.entities.MySynonymEntity"  cascade="refresh" foreign-key="MY_SYNM_FKC" not-null="false" lazy="proxy" fetch="select">
        <column name="MY_SYNM_FK" not-null="false"  sql-type="NUMBER(19)"  />
    </many-to-one>

Here the mapping file belong to an entity which lives in the true database but refers to an entity whose data is populated from a synonym.

What would be the correct way to setup your mapping files for the hbm2ddl tool when synonyms of involved?

Thanks much, JP

levacjeep
  • 673
  • 3
  • 6
  • 23

2 Answers2

1

I add this answer because this is not an acceptable answer to disable the validation. Sometimes you want to validate the schema.

With Hibernate 5.1, the validation also works with synonyms, one should set the following property hibernate.synonyms to true.

Source: https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl

LaurentG
  • 11,128
  • 9
  • 51
  • 66
  • Unfortunately, this does not solve the problem with jtds 1.3.1 on SQL Server. The MS SQL documentation for sp_tables (which is used to determine what tables are available) states "Returns a list of objects that can be queried in the current environment. This means any table or view, except synonym objects." I have been unable to determine a workaround. – cneff Feb 08 '21 at 15:28
0

I think hbm2ddl works only for tables and not for synonyms.I would suggest to keep value for hibernate.hbm2ddl.auto= "" rather then validation.

Same discussion went in hibernate forums

https://forum.hibernate.org/viewtopic.php?p=2438033

Sunil Kumar
  • 5,477
  • 4
  • 31
  • 38