Please have a look at the following
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.att_marks.students.Attendance_Btech">
<id name="id" type="integer" column="id" >
<generator class="increment"/>
</id>
<property name="year">
<column name="Year" />
</property>
<property name="semister">
<column name="Semister"/>
</property>
<property name="section">
<column name="Section"/>
</property>
<property name="period">
<column name="Period"/>
</property>
<property name="subjectCode">
<column name="Subject_Code"/>
</property>
<property name="date">
<column name="Date"/>
</property>
<property name="status">
<column name="Status"/>
</property>
<union-subclass name="com.att_marks.students.Attendance_Btech_ECE" table="attendance_btech_ece">
</union-subclass>
</class>
</hibernate-mapping>
I have tables named attendance_btech_ece, attendance_btech_cse, attendance_btech_it, and so on. All these tables use the same schema as mentioned above for the Attendance_Btech class. I thought this should work but it isn't.
If the Attendance_Btech_ECE is subclass of Attendance_Btech then should work.
Related
I just learn Hibernate, and have some problems with xml mapping.Error code:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
Part of database which i want to map looks like this
part of db
In java application i have two entity class, for each of this tables. I donot insert getters setter and constructor here for better readability.
public class OccupiedBedsEntity {
private int id;
private Timestamp since;
private Timestamp to;
private int id_room;
}
public class RoomEntity {
private int id;
private int roomNumber;
private Integer numberOfBeds;
private int id_department;
private int idResponsibleDoctor;
private Set beds;
}
Xml mapping looks like this.
<?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 default-cascade="all">
<class name="entity.RoomEntity" table="room" schema="" catalog="medicine">
<id name="id">
<column name="id" sql-type="int unsigned" not-null="true"/>
<generator class="native"/>
</id>
<property name="roomNumber">
<column name="room_number" sql-type="int unsigned" not-null="true"/>
</property>
<property name="numberOfBeds">
<column name="number_of_beds" sql-type="int unsigned"/>
</property>
<property name="idResponsibleDoctor">
<column name="id_responsible_doctor" sql-type="int unsigned" not-null="true"/>
</property>
<property name="id_department">
<column name="id_department" sql-type="int unsigned" not-null="true"/>
</property>
<set name="beds">
<key column="id_room"></key>
<one-to-many class="entity.OccupiedBedsEntity"/>
</set>
</class>
</hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" enter code here"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="all">
<class name="entity.OccupiedBedsEntity" table="occupied_beds" schema="medicine" catalog="medicine">
<id name="id">
<column name="id" sql-type="int unsigned" not-null="true"/>
<generator class="native"/>
</id>
<property name="since">
<column name="since_" sql-type="datetime" not-null="true"/>
</property>
<property name="to">
<column name="to_" sql-type="datetime" not-null="true"/>
</property>
<property name="id_room">
<column name="id_room" sql-type="int unsigned" not-null="true"></column>
</property>
</class>
</hibernate-mapping>
Maybe i have to change something not in xml, but in java classes?
Error was in this line in occupiesBebsEntity xml mapper.
<class name="entity.OccupiedBedsEntity" table="occupied_beds" schema="medicine" catalog="medicine">
Code should looks like this.
<class name="entity.OccupiedBedsEntity" table="occupied_beds" schema="" catalog="medicine">
I have three persistent classes named:shops.class,user.class,vehicle.class and three hibernate mapping file shops.hbm.xml,users.hbm.xml,vehicle.hbm.xml
But i am getting following error in my users and vehicle mappingg file:
The content of element type "class" must match
"(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
shops.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 7, 2017 2:04:29 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="persistent_classes.shops" table="SHOPS">
<id name="id" type="int">
<column name="ID" />
<generator class="increment" />
</id>
<version name="version_id" type="int" unsaved-value="undefined">
<column name="VERSION_ID" />
</version>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
<property name="city" type="java.lang.String">
<column name="CITY" />
</property>
<property name="dealername" type="java.lang.String">
<column name="DEALERNAME" />
</property>
<property name="dealerno" type="java.lang.String">
<column name="DEALERNO" />
</property>
<property name="dealeremail" type="java.lang.String">
<column name="DEALEREMAIL" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="shops_string" type="java.lang.String">
<column name="SHOPS_STRING" />
</property>
<property name="login" type="boolean">
<column name="LOGIN" />
</property>
<list name="vehicles" inverse="false" table="VEHICLE" lazy="true">
<key>
<column name="store_id" />
</key>
<list-index></list-index>
<one-to-many class="persistent_classes.vehicle" />
</list>
<map name="users" table="SHOPS_MAP" lazy="true">
<key>
<column name="ID" />
</key>
<map-key type="java.lang.Integer"></map-key>
<element type="java.lang.Integer">
<column name="USERS" />
</element>
</map>
</class>
</hibernate-mapping>
vehicle.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 7, 2017 2:04:29 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="persistent_classes.vehicle" table="VEHICLE">
<id name="id" type="int">
<column name="ID" />
<generator class="increment" />
</id>
<property name="stock" type="int">
<column name="STOCK" />
</property>
<version name="version_id" type="int" unsaved-value="undefined">
<column name="VERSION_ID" />
</version>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="company" type="java.lang.String">
<column name="COMPANY" />
</property>
<property name="milage" type="java.lang.String">
<column name="MILAGE" />
</property>
<property name="year" type="java.lang.String">
<column name="YEAR" />
</property>
<property name="priceoriginal" type="java.lang.String">
<column name="PRICEORIGINAL" />
</property>
<property name="priceoffer" type="java.lang.String">
<column name="PRICEOFFER" />
</property>
<property name="vehicle_string" type="java.lang.String">
<column name="VEHICLE_STRING" />
</property>
</class>
</hibernate-mapping>
users.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 7, 2017 2:04:40 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="persistent_classes.users" table="USERS">
<id name="id" type="int">
<column name="ID" />
<generator class="increment" />
</id>
<map name="points" table="user_points" cascade="all">
<key column="user_id" />
<index column="shop_id" type="integer"></index>
<element column="points" type="string"></element>
</map>
<version name="version_id" type="int" unsaved-value="undefined">
<column name="VERSION_ID" />
</version>
<property name="login" type="boolean">
<column name="LOGIN" />
</property>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="mobno" type="java.lang.String">
<column name="MOBNO" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
<property name="users_string" type="java.lang.String">
<column name="USERS_STRING" />
</property>
</class>
</hibernate-mapping>
I know this question is asked many times but all of that were related to not using tag. But i had used this tag in all files but still getting this error in users and vehicle file.
Please Help i am badly stuck with this.
Thanks in advance
version must be defined before property and map
Move it to be right after id definition in the users and vehicle hbm
I have an AbstractAccount hbm file where i set accountNumber as primarykey. I'am extending this class in my Account.hbm.xml file where I want to over ride this primary key as countryCode and accountNumber composite key. How can i do this?
Abstract.hbm.xml file.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.*.*.*.AbstractAccount"
abstract="true"
table="ACCOUNT"
lazy="false">
<id name="accountNumber" type="java.lang.String">
<column name="ACCOUNT_NUMBER" length="16" />
<generator class="assigned" />
</id>
<discriminator column="ACCOUNT_TYPE" type="string"/>
................
</class>
</hibernate-mapping>
Account.hbm.xml
<property name="modifiedDate" type="java.util.Date">
<column name="MODIFIED_DATE"/>
</property>
<property name="countryCode" type="java.lang.String">
<column name="COUNTRY_CODE"/>
</property>
...............
</subclass>
I have tried with adding composite key as below, but it is not working.
<composite-id name="account_country">
<key-property name="countryCode" column="COUNTRY_CODE" />
<key-property name="accountNumber" column="ACCOUNT_NUMBER" />
</composite-id>
Abstract.hbm.xml
<hibernate-mapping>
/* Abstract class properites */
<joined-subclass name="Account" table="tablename">
/* Account class properites */
</join-subclass>
</hibernate-mapping>
I am trying to do a one-to-many relationship between two tables. I have a table called users that has many user_history entries. I defined these tables in mysql and I generated the entities with intellij hibrernate support. The problem is that when i want to insert something in the databse I am receiving the following error Repeated column in mapping for entity: com.UserHistoryEntity
User:
<?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 name="com.UsersEntity" table="users" schema="" catalog="protein_tracker">
<id name="id">
<column name="id" sql-type="int" not-null="true"/>
</id>
<property name="name">
<column name="name" sql-type="varchar" length="45" not-null="true"/>
</property>
<property name="total">
<column name="total" sql-type="int" not-null="true"/>
</property>
<property name="goal">
<column name="goal" sql-type="int" not-null="true"/>
</property>
<set name="userHistoriesById" inverse="true">
<key>
<column name="id_user" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="com.UserHistoryEntity"/>
</set>
</class>
</hibernate-mapping>
UserHistory:
<?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 name="com.UserHistoryEntity" table="user_history" schema="" catalog="protein_tracker">
<id name="id">
<column name="id" sql-type="int" not-null="true"/>
</id>
<property name="entryDate">
<column name="entry_date" sql-type="datetime" not-null="true"/>
</property>
<property name="entry">
<column name="entry" sql-type="varchar" length="45" not-null="true"/>
</property>
<property name="idUser">
<column name="id_user" sql-type="int" not-null="true"/>
</property>
<many-to-one name="usersByIdUser" class="com.UsersEntity">
<column name="id_user" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
How can I fix this issue?
Mark your Many-To-One mapping for usersByIdUser to insertable = false and updatable = false. This allows the relationship to only be managed by a single property, idUser that you have mapped.
I'm building a data warehouse and want to use InfiniDB as the storage engine. However, it doesn't allow primary keys or foreign key constraints (or any constraints for that matter).
Hibernate complains "The database returned no natively generated identity value" when I perform an insert.
Each table is relational, and contains a unique integer column that was previously used as the primary key - I want to keep that, but just not have the constraint in the db that the column is the primary key.
I'm assuming the problem is that Hibernate expects the db to return a generated key. Is it possible to override this behaviour so I can set the primary key field's value myself and keep Hibernate happy?
-- edit --
Two of the mappings are as follows:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visitor" table="visitor" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="firstSeen" type="timestamp">
<column name="first_seen" length="19" />
</property>
<property name="lastSeen" type="timestamp">
<column name="last_seen" length="19" />
</property>
<property name="sessionId" type="string">
<column name="session_id" length="26" unique="true" />
</property>
<property name="userId" type="java.lang.Long">
<column name="user_id" />
</property>
<set name="visits" inverse="true">
<key>
<column name="visitor_id" />
</key>
<one-to-many class="com.example.project.Visit" />
</set>
</class>
</hibernate-mapping>
and:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visit" table="visit" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="visitor" class="com.example.project.Visitor" fetch="join" cascade="all">
<column name="visitor_id" />
</many-to-one>
<property name="visitId" type="string">
<column name="visit_id" length="20" unique="true" />
</property>
<property name="startTime" type="timestamp">
<column name="start_time" length="19" />
</property>
<property name="endTime" type="timestamp">
<column name="end_time" length="19" />
</property>
<property name="userAgent" type="string">
<column name="user_agent" length="65535" />
</property>
<set name="pageViews" inverse="true">
<key>
<column name="visit_id" />
</key>
<one-to-many class="com.example.project.PageView" />
</set>
</class>
</hibernate-mapping>
As you noted in a comment, there're many id generators you can use. E.g., you many find 'increment' convenient. Complete overview
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id
And hibernate won't care about your db restrictions (both primary keys and foreign keys). In fact, hibernate has no way of knowing about certain db restriction until it's violated. And if restriction doesn't exist, it can never be violated :)
You can remove the <generator class="identity" /> element and set the id manually before saving the object.