1

i deploy oracle database and create 2 schema

  • core
  • msg

c_user table is in core and i wanna create m_message table in msg schema.
my application structure is:

  • Core-Project that is a independent dependency and includes in other projects and User.hbm.xml is there in it.
  • i add Core-Project into pom.xml of Message-Project. Message-Project has Message.hbm.xml.

i use hibernate 4 and my hibernate mapping file is somethings like below. when i start my application (Message-Project), i wanna to m_message created foreign key with c_user table that is in core schema but hibernate generated DDL is wrong. i think hibernate cannot set default_schema properties in User.hbm.xml that is not schema attribute!
Note. i don't wanna to add a schema attribute in User.hbm.xml because Core-Project added into more than 10 projects.

Message.hbm.xml in message-project

<class name="org.message.model.Message" table="m_message" schema="msg">
    <many-to-one name="sender" column="sender_Id" entity-name="org.core.model.User" not-null="true" />
    ...
</class>

User.hbm.xml in core-project

<class name="org.core.model.User" table="c_user">
    ...
</class>

oracle-hibernate.properties

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:HRM
hibernate.connection.username=msg
hibernate.connection.password=msg
hibernate.connection.internal_logon=normal
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl_auto=update
hibernate.default_schema=core

hibernate XML configuration file

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource_" ref="dataSource_" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.default_schema"> ${hibernate.default_schema}</prop>
            ...
         </props>
     </property>
</bean>

hibernate generated DDL for foreign key in something like below:

alter table msg.m_message add constraint FK_filpe81gwdf3f6oqn54d5ybh3 
foreign key (sender_Id) references msg.c_user

why msg.c_user? why hibernate cannot set default_schema for tables that haven't schema attribute?
i think the order of using default_schema for generate foreign key is :

  • 1) using schema of User.hbm.xml and then if not exists
  • 2) using schema of Message.hbm.xml and then if not exists
  • 3) using the default_schema of oracle-hibernate.properties

how can i change this order? thanks a lot...

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Mohsen Mahmoudi
  • 127
  • 1
  • 16
  • I think that the problem is, you are passing the value to the properties file, but it is not loaded to your hibernate configuration XML file. Try to look at this: http://stackoverflow.com/questions/2737420/how-to-set-up-default-schema-name-in-jpa-configuration – holmicz Feb 15 '17 at 07:00
  • @holmicz thanks for replay. but it is OK and default_schema loaded in my hibernate configuration XML file. – Mohsen Mahmoudi Feb 15 '17 at 07:05
  • I don't know, whether you are using spring, but if yes, consider looking at this http://stackoverflow.com/questions/24846636/hibernate-default-schema-not-working . Also have you tried to set schema in User.hbm.xml to core, just to see, whether it is working? And throwing away the "msg" schema from message.hbm.xml, to see if the default schema is put there? – holmicz Feb 15 '17 at 07:13
  • i tested this scenario. when User.hbm.xml has a 'core' schema, and Message.hbm.xml has not 'msg' schema , hibernate add message table in core schema and this means that default_schema is working. but order of using this properties is different..! – Mohsen Mahmoudi Feb 15 '17 at 07:28

1 Answers1

2

Most likely this is a bug. However, even if it is, it will not be fixed in Hibernate 4.

If you can replicate it with Hibernate 5.2, then you should open a new Jira issue, and the issue will be fixed.

However, relying on the HBM2DDL for your production database is much more of an issue. HBM2DDL is good for testing and prototyping, not for production environments. You should use Flyway to manage your database schema.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • thank for replay, i tested this scenario with hibernate 5.2 and if exists, then i create issue. and your sentences about environments is correct. – Mohsen Mahmoudi Feb 15 '17 at 08:06
  • Sure, thanks. Please use [our templates](http://in.relation.to/2016/01/14/hibernate-jpa-test-case-template/) when trying to replicate the issue. – Vlad Mihalcea Feb 15 '17 at 08:22