1

I have table called employee_project on my database which is already made with the following columnEMPLOYEE_NUMBER,PROJECT_CODE,START_DATE,END_DATE,PROJECT_ROLE. There is one to many relationship in between the employee and project for that i have used

private Set<EmployeeProject> employeeProjects = new HashSet<EmployeeProject>();

in Employee pojo class because of this collection, some column is extended in my employee_project table which are employeeProject_PROJECT_CODE,employeeProject_EMPLOYEE_NUMBER. Is there any way to restrict hibernate not to extend the column cause the table is already made? One more thing in project table there is composite id named EMPLOYEE_NUMBER.PROJECT_CODE .One more question is why hibernate create extra table for collection element or extend the column in existing table?

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.nousinfo.tutorial.model">
    <class name="Employee" table="employee">
        <meta attribute="class-description">
            This class contains the employee detail
        </meta>
        <id name="employeeNumber" type="int" column="EMPLOYEE_NUMBER">

        </id>
        <property name="firstName" type="string" column="FIRST_NAME"></property>
        <property name="lastName" type="string" column="LAST_NAME"></property>
        <property name="title" type="string" column="TITLE"></property>
        <property name="departmentId" type="string" column="DEPARTMENT_ID"></property>
        <set name="employeeProjects" cascade="delete" 
            inverse="false">
            <key column="EMPLOYEE_NUMBER" />
            <one-to-many class="com.nousinfo.tutorial.model.EmployeeProject" />
        </set>
        <property name="address1" type="string" column="ADDRESS_1"></property>

    </class>

</hibernate-mapping>

project.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.nousinfo.tutorial.model">
    <class name="EmployeeProject" table="employee_project">
        <meta attribute="class-description">
            This class contains the employee detail
        </meta>
        <composite-id>
            <key-property name="employeeNumber" type="int"
                column="EMPLOYEE_NUMBER"></key-property>
            <key-property name="projectCode" type="string" column="PROJECT_CODE"></key-property>
        </composite-id>
        <property name="startDate" type="date" column="START_DATE"></property>
        <property name="endDate" type="date" column="END_DATE"></property>
        <property name="role" type="string" column="PROJECT_ROLE"></property>
        <many-to-one name="employee" column="EMPLOYEE_NUMBER" class="com.nousinfo.tutorial.model.Employee" not-null="true" insert="false" update="false" ></many-to-one>
    </class>
</hibernate-mapping
henrycharles
  • 1,019
  • 6
  • 28
  • 66

0 Answers0