문제

Hibernate mapping different Table to one POJO class

I have requirement to map two different tables to one POJO class in hibernate.

I have tried configuring hibernate mapping using join but I get error like

DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704.

My table relation is shown in the figure.

enter image description here

My hibernate mapping is below.

<hibernate-mapping>
    <class name="com.mydomain.Student" table="STUDENT_TABLE" schema="UNIV">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>

        <property name="studentId" type="java.lang.Integer">
            <column name="STUDENT_ID" />
        </property>

        <property name="course" type="java.lang.Integer">
            <column name="COURSE_ID" />
        </property>

        <join table="COURSE_TABLE"  >  
            <key column="COURSE_ID" />    
                <property name="courseName" type="string">
                  <column name="COURSE_NAM" />
                        </property>  

                        <property name="courseTeacher" type="string">
                    <column name="COURSE_Teacher" />
                         </property>            

        </join>

   </class>

</hibernate-mapping>

My student class has getter and setter for.

id
studentId
    course
courseName
courseTeacher

Can somebody help me to fix this mapping issue?

올바른 솔루션이 없습니다

다른 팁

After seeing your table relation diagram. Your database designing is wrong.

One student can enroll for more than 1 course. And 1 course can contain more than 1 students.

So basically its a many-many mapping.

Here are some examples for you to achieve it.

Example1

Example2

UPDATE

I was improvising your design the correct way. But if you still insist that its 0ne-one mapping, then here is a similar question with answer. You will require id's to be exact so that it corresponds to one record only. Have look at hibernate-doc for the same.

Have you verified the names of all your columns are correct in the hibernate mapping? Looking up the error code -204, it says "name IS AN UNDEFINED NAME."

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top