How to create table from xml in Hibernate 5? - java

I keep table schema in this file tableaddress.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1"
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<entity class="Address">
<attributes>
<basic name="city" attribute-type="String" />
<basic name="country" attribute-type="int" />
<basic name="province" attribute-type="double" />
<basic name="postalCode" attribute-type="boolean">
</basic>
<basic name="street" attribute-type="String" />
</attributes>
</entity>
</entity-mappings>
Here is how I tried to create table using Hibernate
public class App
{
public static void main( String[] args )
{
Properties prop= new Properties();
prop.setProperty("hibernate.connection.url", "jdbc:mariadb:......");
prop.setProperty("dialect", "org.hibernate.dialect.MariaDB53Dialect");
prop.setProperty("hibernate.connection.username", "user");
prop.setProperty("hibernate.connection.password", "password");
prop.setProperty("hibernate.connection.driver_class", "org.mariadb.jdbc.Driver");
SessionFactory sessionFactory = new Configuration()
.addResource("tableaddress.orm.xml").addProperties(prop).buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.getTransaction().commit();
session.close();
}
}
It should work, there is no compile errors, but for some reason the table is not created
Error java.lang.NoClassDefFoundError: javax/transaction/SystemException

This is probably due to bug/feature in Hibernate.
The javax.transaction was removed from Hibernate (well, marked as "provided") in 5.0.4, but it was brought back in 5.0.7 (see https://hibernate.atlassian.net/browse/HHH-10307 :
"JTA no longer transitively provided (HHH-10178) causes problems for
apps not using JTA"
)
So assuming you are using 5.04, 5.0.5 or 5.0.6, your choices are to either upgrade Hibernate, or to add the following dependency :
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

Related

Hibernate saveOrUpdate Not working with Dynamic mapping

I am using Hibernate dynamic modeling for persisting my objects. The issue i am facing right now is it is throwing exception when i am calling saveOrUpdate function. The exception is
2020-07-08 23:14:06 WARN SqlExceptionHelper:137 - SQL Error: 1062, SQLState: 23000
2020-07-08 23:14:06 ERROR SqlExceptionHelper:142 - Duplicate entry 'TYUTQ-2020-07-08 20:19:53' for key 'PRIMARY'
2020-07-08 23:14:06 ERROR ExceptionMapperStandardImpl:39 - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
When object is going to insert first time, it will successfully inserted but when object is already present in the database then saveOrUpdate method again call the insert method. I verified by using SHOW_SQL = "true" property.
The question is why it is not calling the update method?
Hibernate mapping file is :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="myTable">
<composite-id>
<key-property name="code" column="code" type="java.lang.String" />
<key-property name="datetime" column="datetime" type="java.sql.Timestamp" />
</composite-id>
<property name="A" column="A_colummn" type="java.math.BigDecimal" />
<property name="B" column="B_column" type="java.math.BigDecimal" />
<property name="C" column="C_column" type="java.math.BigDecimal" />
<property name="D" column="D_column" type="java.math.BigDecimal" />
<property name="E" column="E_column" type="java.math.BigDecimal" />
<property name="F" column="F_column" type="int" />
<property name="G" column="G_column" type="java.sql.Timestamp" />
</class>
</hibernate-mapping>
And my code is
List<Map<String, Object>> toBeSaved = getTransformedObjects();// all objects belong to same table
Transaction tx = null;
try (SessionFactory factory = HibernateUtils.getSessionFactoryFromXml(profile, job.getXmlMapping());// Creating Session from XML in my utils class
Session session = factory.openSession()) {
tx = session.beginTransaction();
toBeSaved.forEach(map -> {
session.saveOrUpdate(job.getTableName(), map);
});
tx.commit();// Here exception occurs
} catch (HibernateException e) {
rollbackTransactionAndPrintException(e, destTx);
}
Currently there is only one record in map and content of map is :
{
datetime=2020-07-08 20:19:53.21,
F_column=1,
A_colummn=0.421,
G_column=2020-06-16 17:21:05.0,
code=TYUTQ,
B_column=0.000,
C_column=0.000,
D_column=50.540
}
i am using hibernate version 5.3.5.Final and i am badly stuck in it.
Can anyone tell me how to resolve this ? Thanks in advance.

org.hibernate.HibernateException: could not determine type of dynamic entity

I am a beginner in Hibernate and I have the below code in one of my projects:
ShortHand o = new ShortHand();
Session sess = noprobs.getSessionFactory().openSession();
sess.getTransaction().setTimeout(noprobs.xActionTimeout);
tx = sess.beginTransaction();
sess.save(o);
For which I get the below error:
ERROR:
org.hibernate.HibernateException: could not determine type of dynamic entity
at org.hibernate.impl.SessionImpl.guessEntityName(SessionImpl.java:1770)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at cool.wxhy.tms.xyz.ABCD.persistInDb(ABCD.java:235)
at cool.wxhy.tms.xyz.ABCD.subscribeToAmps(ABCD.java:144)
at cool.wxhy.tms.xyz.ABCD.hot(ABCD.java:102)
at cool.wxhy.service.impl.wxhyBase.start(wxhyBase.java:250)
at cool.wxhy.service.api.hello.startAllServices(hello.java:211)
at cool.wxhy.service.impl.heyhi.main(heyhi.java:43)
I am using the HIbernate 3.2.6 jar
I am using the xml config for the HIbernate and not the annotations one. Can someone please help on the issue.
Hibernate Mapping file:
<hibernate-mapping package="bofa.pioneer.tms.objects">
<class name="ShortHand" table="ShortHand">
<id name="key" column="id" unsaved-value="null" type="string" >
<generator class="assigned" />
</id>
<property name="animal" column="animal" type="string"/>
<property name="dog" column="dog" type="string"/>
Entity
public class ShortHand extends LongHand {
//getters and setters are present in the parent LongHand
}

Axon - JPA Event store for MySql

I'm trying to implement Axon app using JPA Event Store and mysql database. Hibernate is automatically generating all the tables and it works OK so far.
My question is - can I replace hibernate_sequence mysql table for AUTO_INCREMENT columns in Mysql. I guess in order to do this I would need to modify source code of Axon, since I cannot find other configurable way to modify #Id annotation for Domain event #Entity or other entitites?
UPDATE
OK, i managed to do it by placing new file in src\main\resources\META-INF\orm.xml with following code:
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<mapped-superclass class="org.axonframework.eventhandling.AbstractSequencedDomainEventEntry" access="FIELD">
<attributes>
<id name="globalIndex">
<generated-value strategy="IDENTITY"/>
</id>
</attributes>
</mapped-superclass>
<entity class="org.axonframework.modelling.saga.repository.jpa.AssociationValueEntry" access="FIELD">
<attributes>
<id name="id">
<generated-value strategy="IDENTITY"/>
</id>
</attributes>
</entity>
</entity-mappings>
You could make this adjustment through code, that's true.
It is however more straightforward to specify an orm.xml file in your project which for some tables (the domain_event_entry table in your scenario I assume) can adjust certain columns.
In there you should be able to adjust the sequence generator to what you desire it to be.
Hope this helps!

dropwizard multiple tables

How would I create two tables in a single database in dropwizard?
In my run method I have:
public void run(HelloWorldConfiguration configuration, Environment environment) {
final PersonDAO dao = new PersonDAO(hibernateBundle.getSessionFactory());
final LADAO dao2 = new LADAO(hibernateBundle.getSessionFactory());
environment.jersey().register(new ProtectedResource());
environment.jersey().register(new PeopleResource(dao));
environment.jersey().register(new PersonResource(dao));
environment.jersey().register(new LAResource(dao2));}
Tables are created in your migrations.xml file
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
<changeSet id="1" author="people">
<!-- first table created -->
<createTable tableName="table1">
<column name="jobTitle" type="varchar(255)"/>
</createTable>
<!-- second table created -->
<createTable tableName="table2">
<column name="whatever" type="varchar(255)"/>
</createTable>
</changeSet>
</databaseChangeLog>
Then use the 'db migrate' command to run the migration:
java -jar hello-world.jar db migrate helloworld.yml

JPA xml mapping not finding fields

I have the following class:
package lt.vic.valdos.domain.valda;
public class Valda implements java.io.Serializable {
private long id;
private Long valdosKodas;
public long getId() {
return id;
}
public Long getValdosKodas() {
return valdosKodas;
}
}
and the following orm.xml:
<?xml version="1.0"?>
<entity-mappings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm http://www.eclipse.org/eclipselink/xsds/eclipselink_orm_2_1.xsd"
version="2.1">
<entity class="lt.vic.valdos.domain.valda.Valda">
<table name="VALDOS" schema="VLD" />
<attributes>
<id name="id" />
<basic name="id">
<column name="vld_id" />
<return-insert return-only="true" />
</basic>
<basic name="valdosKodas">
<column name="valdos_kodas" />
</basic>
</attributes>
</entity>
</entity-mappings>
When I deploy this in glassfish, i get the following error:
Exception [EclipseLink-7215] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Could not load the field named [id] on the class [class lt.vic.valdos.domain.valda.Valda]. Ensure there is a corresponding field with that name defined on the class.
The class is in a jar that is included into a web application as a maven dependency. The orm.xml is in /WEB-INF/classes/META-INF of the web application.
What am I doing wrong?
Figured this one out myself. For some reason EclipseLink requires a setter on a class. Once I add private setters everything seems fine. Why the setters are needed (mapping accessors should default to FIELD) remains a mystery but it is not that important for me. Adding access="FIELD" to all entity attributes also fixes the problem without the setters.
You should be specifying the id as generated using the IDENTITY strategy:
<id name="id">
<column name="vld_id"/>
<generated-value strategy="IDENTITY"/>
</id>
This strategy will automatically read the database provided id back into the new object upon successful commit. The EclipseLink returning statement functionality is only applicable to basic mappings because id is already covered by Identity ID generation.
I think you have to add the column description for the id column to your id element, instead of using an extra basic element. As in <id name="id"> <column name="vld_id" /> ... </id>, without an extra <basic name="id"> ....
From my own experience (some time ago now), it's probably easier to use annotations to define your mappings.

Categories

Resources