Error with parsing Hibernate Man.hbm.xml - java

I can not run my hibernate application. I'm constantly getting this error:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/MAG/.m2/repository/org/slf4j/slf4j-jdk14/1.7.2/slf4j-jdk14-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/MAG/.m2/repository/org/slf4j/slf4j-log4j12/1.5.8/slf4j-log4j12-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]
2013-02-14 14:49:05 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.3.0.SP1
2013-02-14 14:49:05 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
2013-02-14 14:49:05 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : javassist
2013-02-14 14:49:05 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
2013-02-14 14:49:05 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
2013-02-14 14:49:05 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
2013-02-14 14:49:05 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : model/man/Man.hbm.xml
2013-02-14 14:49:06 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(25) Attribute "name" must be declared for element type "many-to-many".
2013-02-14 14:49:06 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(33) Attribute "name" must be declared for element type "many-to-many".
2013-02-14 14:49:06 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(38) The content of element type "set" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)".
The idea of my program is that:
Man is super class for Owner and Renter.
Owner can have many Flats but Flat can have one Owner.
Renter can have many RenterBills but RenterBill can have one Renter.
Renter can have many Flats and Flats can have many renters.
My Man.hbm.xml:
<class name="Man" table="MEN">
<id name="id" column="MAN_ID">
<generator class="native" />
</id>
<property name="pesel" column="MAN_PESEL" />
<property name="idNumber" column="MAN_ID_NUMBER" />
<property name="email" column="MAN_EMAIL" />
<property name="name" column="MAN_NAME" />
<property name="surname" column="MAN_SURNAME" />
<property name="telephoneNumber" column="MAN_TELEPHONE_NUMBER" />
<many-to-one name="address" column="ADDRESS_ID" not-null="true" />
<joined-subclass name="Owner" table="OWNERS">
<key column="MAN_ID" />
<property name="password" column="OWNER_PASSWORD" not-null="true" />
<property name="seed" column="OWNER_SEED" not-null="true" />
<set name="flats" table="OWNER_FLATS">
<key column="MAN_ID" />
<many-to-many name="flats" column="FLAT_ID" class="Flat" />
</set>
</joined-subclass>
<joined-subclass name="Renter" table="RENTERS">
<key column="MAN_ID" />
<set name="flats" table="RENTER_FLATS">
<key column="MAN_ID" />
<many-to-many name="flats" column="FLAT_ID" class="Flat" />
</set>
<set name="bills" table="RENTER_BILLS">
<key column="MAN_ID" />
<many-to-one name="bills" column="RENTER_BILL_ID" class="RenterBill" />
</set>
</joined-subclass>
</class>
Java classes:
Man.java
package model.man;
import model.addresses.Address;
public abstract class Man {
private int id;
private String pesel;
private String idNumber;
private String email;
private String name;
private String surname;
private String telephoneNumber;
private Address address;
/* getters setters */
}
Owner.java
package model.man;
import java.util.Iterator;
import java.util.Set;
import model.flat.Flat;
public class Owner extends Man implements Iterable<Flat> {
private String password;
private String seed;
private Set<Flat> flats;
/* getters setters */
}
Renter.java
package model.man;
import java.util.HashSet;
import java.util.Set;
import model.bills.RenterBill;
import model.flat.Flat;
public class Renter extends Man {
private Set<Flat> flats = new HashSet<Flat>();
private Set<RenterBill> bills = new HashSet<RenterBill>();
/* getters setters */
}

SEVERE: Error parsing XML: XML InputStream(25) Attribute "name" must be declared for element type "many-to-many".
2013-02-14 14:49:06 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(33) Attribute "name" must be declared for element type "many-to-many".
2013-02-14 14:49:06 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(38) The content of element type "set" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)".
Try removing the 'name' attribute from your many-to-many tag.
UPDATE
You have a set mapped as a many-to-one as well. This typically shows up in your Java POJO as a single instance of the referenced class. You may have meant to use a one-to-many tag there.

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
}

Hibernate: Issue with mapping Enum Property Type

Followed every step mentioned in this tutorial but still getting following exception:
Caused by: org.hibernate.MappingException: Unable to instantiate custom type: org.hibernate.type.EnumType
Caused by: java.lang.NullPointerException
at org.hibernate.type.EnumType.setParameterValues(EnumType.java:203) [hibernate-core-4.2.0.Final.jar:4.2.0.Final]
at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:131) [hibernate-core-4.2.0.Final.jar:4.2.0.Final]
Part 2
Then I tried using #Enumerated itself. Changed hbm to <property name="gender" column="usr_gender" not-null="true" type="org.hibernate.type.EnumType">.
Entity class:
import javax.persistence.Enumerated;
import javax.persistence.EnumType;
#Enumerated(EnumType.STRING)
Gender gender
Still getting the same error.
Part 3
Update hbm property to <property name="gender" column="usr_gender" not-null="true" type="string">.
Kept entity class same.
Got back to old error.
Expected type: Gender, actual value: java.lang.String
hbm file
<property name="shortName" column="short_name" type="string" not-null="true"/>
<property name="description" column="description" type="string" not-null="true"/>
<property name="gender" column="gender_type" not-null="true" type="string"/>
Entity Class
import javax.persistence.Enumerated;
import javax.persistence.EnumType;
private String shortName;
private String description;
#Enumerated(EnumType.STRING)
private Tenant tenant;
I got the same exception when I tried to create a custom type for my enum field to give it as the type argument to the addScalar() method for an SQLQuery. It is not clear which part of your code throws the exception. But it seems that you are trying to create a custom type to overcome exception due to argument mismatch of setter method for an enum on result transformation. Please see my answer to Hibernate SQL transformation fails for Enum type field.

Hibernate/Oracle Sequence not working

I am using hibernate and an oracle DB to try and insert a automated ID into a table using a sequence. The sequence defiantly exists on database but hibernate doesn't seem to be able to find it.
Here is all the relevant information:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
org.hibernate.exception.SQLGrammarException: could not get next sequence value
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
....
Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist
....
... 12 more
I know it says "Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist" but I can access the sequence on the database:
Table:
CREATE TABLE Property(
id INT,
address VARCHAR2(50),
town VARCHAR2(50),
postCode VARCHAR2(50),
purchasePrice INT
);
Sequence:
create sequence property_seq start with 1 increment by 1 nomaxvalue;
Mapping xml:
<class name="com.rental.model.property.Property" table="PROPERTY">
<meta attribute="class-description"> This class contains the property detail. </meta>
<id name="id" type="integer" column="id">
<generator class="sequence"/>
</id>
<property name="address" column="ADDRESS" type="string" />
<property name="town" column="TOWN" type="string" />
<property name="postCode" column="POSTCODE" type="string" />
<property name="purchasePrice" column="PURCHASEPRICE" type="integer" />
</class>
annotation:
#Id
#SequenceGenerator(name="property_seq", sequenceName="property_seq", allocationSize=1, initialValue=1)
#GeneratedValue (strategy = GenerationType.SEQUENCE, generator="property_seq")
public int getId() {
return id;
}
Why are you using xml AND #Annotation at the same time? Maybe xml definition wins against annotation and Hibernate is retriving default sequence instead of your property_seq.
Try remove xml mapping and check if it works.

MappingException .hibernate mapping fails for classes in different packages

while using hibernate,I came across this problem mapping classes in two different packages.
I have a myapp.domain.ItemforSale class which is to be mapped to myapp.cart.CartItem
I created CartItem.hbm.xml as below
<hibernate-mapping package="myapp.cart">
<class name="CartItem" table="CARTITEM">
<id name="cartItem_id" column="CARTITEM_ID" type="long">
<generator class="native"/>
</id>
<property name="quantity" type="int" column="QUANTITY" />
<many-to-one name="itemforsale" class="myapp.domain.ItemforSale" column="ITEM_FORSALE_ID" lazy="false" />
</class>
...
I have a unidirectional mapping from CartItem to ItemForSale entity.While running a test on saving ItemForSale instances to db..I got this error
506 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
506 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
571 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : myapp/domain/ItemForSale.hbm.xml
636 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: bookshop.domain.ItemForSale ->ITEMFORSALE
...
746 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : myapp/cart/CartItem.hbm.xml
751 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: myapp.cart.CartItem -> CARTITEM
...
org.hibernate.MappingException: Association references unmapped class: myapp.domain.CartItem
I removed the package attribute from element and put fully classified class names.Still I get the same exception..
<hibernate-mapping>
<class name="myapp.cart.CartItem" table="CARTITEM">
<id name="cartItem_id" column="CARTITEM_ID" type="long">
<generator class="native"/>
</id>
<property name="quantity" type="int" column="QUANTITY" />
<many-to-one name="itemforsale" class="myapp.domain.ItemForSale" column="ITEM_FORSALE_ID" lazy="false" />
</class>
As a final attempt,I moved the CartItem to myapp.domain package and everything works!
Any idea why this happens?It won't make sense in my app to move myapp.cart.CartItem to myapp.domain ..Any help greatly appreciated.
thanks
mark
org.hibernate.MappingException: Association references unmapped class: myapp.domain.CartItem
This only means it is searching for CartItem on myapp.domain package but your class is actually in myapp.cart package. So basically the hibernate mapping that you have shown is correct, but I think there could be some issues on your java class. Could you please display your java classes related to your issue?
Or you haven't display other related hibernate mapping that could cause this exception.
Also make sure all mappings in hibernate.cfg.xml or Spring's applicationContext corresponds to actual mappings.

Categories

Resources