I am trying to generate hbm.xml files to support the dynamic map mode of Hibernate. When I run hbm2hbmxml my classes are generated with the name attribute instead of the entity-name attribute. When I try to use dynamic map, Hibernate fails to load data from the DB because it cannot find the class. I can manually change the it from name to entity-name and it works.
The actual output is:
<hibernate-mapping>
<class name="MyEntity" table="MyEntity" schema="dbo" catalog="MyDB">
...
</class>
</hibernate-mapping>
The necessary output should be:
<hibernate-mapping>
<class entity-name="MyEntity" table="MyEntity" schema="dbo" catalog="MyDB">
...
</class>
</hibernate-mapping>
My hibernate.properties file contains:
hibernate.default_entity_mode=dynamic-map
I am running hbm2hbmxml using hibernate3-maven-plugin version 2.2.
I would like some help configuring the hbm2hbmxml plugin such that it generates my classes with the entity-name attribute.
Thanks for the help.
Related
I am new to liquibase. The 'backend' app runs locally with some basic changelog files.
I would like to get some test data from a server using pg_dump and pg_restore, and restore it to my local postgres DB.
How do I get that to work with liduibase? If that is not a good option, what would be a better option?
I found out a good way to export an entire database data into csv, is to use the below pg function, configure changelog to load the csv files, and map column headers if records have a lot of null data.
Create one csv file per table:
Export Database into CSV file
Add changelog LoadData
(the file path is relative to src/main/resources):
for example:
<changeSet author="programmer" id="mock_user_data">
<loadData tableName="user_data" file="db/csv/public.user_data.csv" separator=";">
<column name="default_user" type="BOOLEAN"/>
<column name="username" type="STRING"/>
<column name="store_id" type="NUMERIC"/>
</loadData> </changeSet>
If you get error messages caused by null values, look at your csv file, insert NULL in between delimiters, and make sure the problematic column name is mapped in the loadData section.
you could add a changelog to load data
Load data
you could possibly export from your DB as a csv and load with liquibase
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...
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.
I have a project using Hibernate and external XML mapping files. I switched from MySQL to Oracle. Some of my fields have the name 'date', which is okay in MySQL but not in
Oracle. It does not like
<property name="date" column="date" type="string" />
so I changed it to
<property name="sdate" column="sdate" type="string" />
When I re-rerun the code to generate the schema, it is still following the old version of the mapping file and not taking into account the new changes. I have even created a similar but different xml file and pointed my Hibernate config to this new file and it has the same problem.
Does anyone know why it could be following the old version of my mapping file and refusing to follow my updates?
I am trying to use Hibernate 3.2.5 with Play framework 1.2.5
In Hibernate I am having two files:
1) cfg.xml file (containing the db config details along with some additional properties
2) hbm.xml file (containing the mapping between the java bean and the db table
For getting connected to the oracle 10g db, I am providing the db details in the application.config files like this and the connection is successful also when I start the server:
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=system
db.pass=tiger
I want to know Where will I place the hbm.xml file (for mapping details) and the cfg.xml file for the remaining properties other than db connecion details?
Please let me know about this.
Regards,
Starting from the root directory of your application:
the hibernate.cfg.xml must be placed inside the app directory
the mapping files (the hbm files) where your models classes are defined, usually inside the app/models/ directory
Inside your hibernate.cfg.xml the mapping attributes should be something like:
<mapping class="models.yourHmbFile1"/>
<mapping class="models.yourHmbFile2"/>
Btw, I find easy to use the hibernate annotations instead of the hbm - xml mapping. Easier to write and to mantain.
If you prefer to annotate your model classes, you can delete the hbm files and directly map your annotated classes in your hibernate.cfg.xml.
In the application.conf you've to specify the data you have already added:
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=system
db.pass=tiger
Also in the hibernate.cfg.xml you need to specify the connection data:
<property name="hibernate.dialect">...</property>
<property name="hibernate.connection.driver_class">...</property>
<property name="hibernate.connection.url">...</property>
<property name="hibernate.connection.username">...</property>
<property name="hibernate.connection.password">...</property>
I am using spring and hibernate. I have mapping locations specified as:
My folder structure is as follows:
src/main/resources
.
...hibernate
.
.....hibernate2
<property name="mappingLocations" value="classpath:hibernate/*.hbm.xml" />
above property is looking into only hibernate folder and is not looking into subfoders. how can i force to find hbm files in sub folders as well?
Insert a wildcard * as below
<property name="mappingLocations" value="classpath*:hibernate/**/*.hbm.xml" />