1

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???

vasanth
  • 224
  • 3
  • 19
  • Did you check http://www.tutorialspoint.com/hibernate/hibernate_one_to_many_mapping.htm – Subodh Joshi Sep 16 '15 at 06:20
  • That doesn't help in my coding.. i need to know where should i make change to map between the hbm files.Thankyou – vasanth Sep 16 '15 at 06:25
  • In this link they mentioned one `Emp` have more than one `certificates`. So `Emp` have `1ToManey Relationship`.Now you have to decide what mapping you are doing. – Subodh Joshi Sep 16 '15 at 06:32
  • ok joshi thanks its many to one but actually problem is i am getting null pointer exception i'm not able to get the value setted in employee class object in admin class – vasanth Sep 16 '15 at 06:47
  • can you post your error trace so that we can understand what is the real problem? – soorapadman Sep 16 '15 at 06:54
  • root cause of the error is:org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into admin (coursename, participants, empid, courseid) values (?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update – vasanth Sep 16 '15 at 07:03

0 Answers0