I need to use many to one relationship between two tables while using the spring hibernate integrated program. I have employee.hbm.xml and admin.hbm.xml which has corresponding bean classes.
empid in employee.hbm.xml acts as a foreign key in admin.hbm.xml .
employee.hbm.xml
<hibernate-mapping>
<class name="entity.Employee" table="employee">
<id name="empid" column="empid" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="empname" column="empname" type="java.lang.String"></property>
<property name="password" column="password" type="java.lang.String"></property>
<property name="designation" column="designation" type="java.lang.String"></property>
<property name="domain" column="domain" type="java.lang.String"></property>
<property name="role" column="role" type="java.lang.String"></property>
<property name="head" column="head" type="java.lang.String"></property>
<property name="specialist" column="specialist" type="java.lang.String"></property>
</class>
</hibernate-mapping>
admin.hbm.xml
<hibernate-mapping>
<class name="entity.Admin" table="admin">
<id name="courseid" column="courseid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="coursename" column="coursename" type="java.lang.String"></property>
<property name="participants" column="participants" type="java.lang.Integer"></property>
<!-- <property name="empid" column="empid" type="java.lang.Integer"/> -->
<many-to-one name="employee" class="entity.Employee" fetch="select"
column="empid" cascade="all"/>
</class>
</hibernate-mapping>
while i try to insert to admin table, i get a NullPointerException on field empid. what should i modify to make foreign key constraint between these two hbm files???
Related
I have simple definition of my entities, but what i do wrong, because i can not get any entity UserInfo and receive only error (could not load an entity: [UserInfo#2]... )?
P.S.
But query what was traced by Hibernate in console is a valid
select
userinfo0_.primarykey as primaryk1_0_1_,
userinfo0_.firstname as firstnam2_0_1_,
userinfo0_.lastname as lastname3_0_1_,
userinfo0_.birthday as birthday4_0_1_,
userinfode1_.user_id as user_id1_1_0_,
userinfode1_.dep_id as dep_id2_1_0_,
userinfode1_.salary as salary3_1_0_,
userinfode1_.prof as prof4_1_0_
from
SCHEMA.UserInfo userinfo0_
left outer join SCHEMA.UserInfoDetail userinfode1_ on userinfo0_.primarykey = userinfode1_.user_id
<hibernate-mapping>
<class entity-name="UserInfo" schema="SCHEMA" table="UserInfo">
<id column="primarykey" name="primarykey" type="long">
<generator class="identity"/>
</id>
<property column="firstname" name="firstname" type="string"/>
<property column="lastname" name="lastname" type="string"/>
<property column="birthday" name="birthday" type="date"/>
<one-to-one name="UserInfoDetail" class="UserInfoDetail" cascade="save-update"/>
</class>
<class entity-name="UserInfoDetail" schema="SCHEMA" table="UserInfoDetail">
<id column="user_id" name="user_id" type="long">
<generator class="foreign">
<param name="property">UserInfo</param>
</generator>
</id>
<property column="prof" name="prof" type="string"/>
<property column="salary" name="salary" type="long"/>
<property column="dep_id" name="dep_id" type="long"/>
<one-to-one name="UserInfo" class="UserInfo" property-ref="UserInfoDetail" constrained="true"/>
</class>
</hibernate-mapping>
Can anyone suggest me how to map two tables in hibernate.
<hibernate-mapping>
<class name="com.git.medicalapp.Pojo.CategoryPojo" table="CategoryPojo">
<id name="categoryId" type="int">
<generator class="native"/>
</id>
<property name="categoryName" type="string"/>
<property name="categoryDescription" type="string"/>
<property name="disease" type="string"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.git.myapp.pojo.Disease" table="Disease">
<id name="diseaseid" type="int">
<generator class="native"/>
</id>
<property name="diseasename" type="string"/>
<property name="symptomsid" type="int"/>
<property name="typeofdisease" type="string"/>
<property name="treatmenttype" type="string"/>
<property name="fees" type="int">
</class>
</hibernate-mapping>
how to make disease name in second hbm as foreign key to first hibernate-mapping block.
Please have a look at the below XML code
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 17, 2015 10:01:43 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="model.main.Family" table="family" catalog="****" optimistic-lock="version">
<id name="idFamily" type="int">
<column name="idFamily" />
<generator class="assigned" />
</id>
<many-to-one name="employee" class="model.main.Employee" fetch="select">
<column name="idEmployee" not-null="true" />
</many-to-one>
<property name="firstName" type="string">
<column name="FirstName" length="45" />
</property>
<property name="middleName" type="string">
<column name="MiddleName" length="45" />
</property>
<property name="lastName" type="string">
<column name="LastName" length="45" />
</property>
<property name="dob" type="date">
<column name="DOB" length="10" />
</property>
<property name="passportNumber" type="string">
<column name="PassportNumber" length="45" not-null="true" />
</property>
<property name="dateLeft" type="date">
<column name="DateLeft" length="10" />
</property>
<property name="lastUpdated" type="timestamp">
<column name="LastUpdated" length="19" not-null="true" />
</property>
<set name="visas" table="visa" inverse="true" lazy="true" fetch="select">
<key>
<column name="idFamily" />
</key>
<one-to-many class="model.main.Visa" />
</set>
</class>
</hibernate-mapping>
It is the Hibernate mapping class of my database table Family. We create the database separately using MySQL Work bench and then generate the mapping classes. We auto generated the mapping files using netbeans as mentioned in "Generating Hibernate Mapping Files and Java Classes" section of netbeans tutorial.
Now we have a problem. That is, we changed the primary key (idFamily) of our table Family to an auto generated field inside MySQL. Now, how can we change the above hibernate code so it identifies the idFamily as an auto generated one?
The other question is, manually editing one mapping class without regenerating all the mappings via a tool can "break" the system? For an example, like messing up with relationships?
In Annotation It work for me as
#GeneratedValue(strategy= GenerationType.IDENTITY)
for you hope it works
<generated-value strategy="IDENTITY" />
You're looking for an identity column. That indicates that the column value is auto-generated as an identity for the row by the RDBMS.
<generator class="identity" />
See the these Hibernate docs for more information. According to it:
Identity
supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
Just replace your generator class to increment it will treat it as autoincrement
<generator class="increment"/>
<?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.tech.spring4.model.User" table="Customer">
<id name="id" type="long">
<column name="USERID" unique="true"/>
<generator class="increment"/>
</id>
<property name="username"><column name="username" length="30" not-null="true"></column></property>
<property name="email"><column name="email" length="100" not-null="true"></column></property>
<property name="address"><column name="address" length="100" not-null="true"></column></property>
</class>
</hibernate-mapping>
The configuration for User class :
<class name="User" table="users" lazy="false">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="type" column="type"/>
<many-to-one name="parent" column="parent"/>
<property name="loginName" column="login_name" unique="true" not-null="true" index="idx_users_login_name" length="50"/>
<property name="name" column="name" length="50"/>
<property name="password" column="password"/>
<property name="email" column="email" length="50"/>
<property name="locale" column="locale" length="20"/>
<property name="locked" column="locked"/>
<many-to-one name="metadata" column="metadata_id"/>
<set name="userSpaceRoles" cascade="all" inverse="true" lazy="false">
<key column="user_id"/>
<one-to-many class="UserSpaceRole"/>
</set>
</class>
and for the class MeetingItem is:
<class name="MeetingItem" table="meeting_item">
<id name="id" column="meeting_item_id" type="long">
<generator class="native"/>
</id>
<property name="summary" column="summary" type="string"/>
<property name="detail" column="detail" type="string"/>
<many-to-one name="space" column="space_id"/>
<property name="date" column="date" type="date"/>
<list name="users" cascade="all" lazy="false">
<key column="meeting_item_id"/>
<index column="idx"/>
<one-to-many class="User"/>
</list>
</class>
The problem is I am getting the exception:
org.hibernate.MappingException: Association references unmapped class: info.domain.User
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2380)
at org.hibernate.cfg.HbmBinder.bindListSecondPass(HbmBinder.java:2231)
at org.hibernate.cfg.HbmBinder$ListSecondPass.secondPass(HbmBinder.java:2729)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.Configuration.generateSchemaUpdateScript(Configuration.java:936)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:140)
The mapping of the list is creating the problem. What I am doing wrong?
Edit:
This two configuration resides in different file, if these two are placed in same xml then the problem is not occurring.
please add a reference to the mapping file (which maps info.domain.User) into hibernate.cfg.xml.
Please add class level annotations to register the class as a Spring bean, i.e #Entity in this case since you are not using xml configuration.
I have a class Auction that contains a Class Item and Users but when I am getting the class, the class item and Users are not being loaded.
Auction Class Mapping 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">
<!-- Generated Dec 28, 2010 9:14:12 PM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.Models.Auction" table="AUCTION">
<id name="AuctionId" type="long">
<column name="AUCTIONID" />
<generator class="native" />
</id>
<property name="StartTime" type="java.util.Date">
<column name="STARTTIME" />
</property>
<property name="EndTime" type="java.util.Date">
<column name="ENDTIME" />
</property>
<property name="StartingBid" type="long">
<column name="STARTINGBID" />
</property>
<property name="MinIncrement" type="long">
<column name="MININCREMENT" />
</property>
<many-to-one name="CurrentItem" class="com.BiddingSystem.Models.Item" fetch="join" cascade="all">
<column name="ItemId" />
</many-to-one>
<property name="AuctionStatus" type="java.lang.String">
<column name="AUCTIONSTATUS" />
</property>
<property name="BestBid" type="long">
<column name="BESTBID" />
</property>
<many-to-one name="User" class="com.BiddingSystem.Models.Users" fetch="join">
<column name="UserId" />
</many-to-one>
</class>
</hibernate-mapping>
When I am doing this:
Query query=session.createQuery("from Auction where UserId="+UserId);
List <Auction> AllAuctions= new LinkedList<Auction>(query.list());
The Users and Item are null
#ManyToOne(fetch=FetchType.EAGER) would be the annotation based configuration element you are missing. Please refer to the manual to see how to configure this by XML.