hibernate中的一对多映射
我的hibernate中的一对多映射,hibernate,join,hibernate-mapping,Hibernate,Join,Hibernate Mapping,我的LevelTerm.hbm.xml文件是: <hibernate-mapping> <class name="com.entity.LevelTerm" table="level_term" catalog="test"> <id name="levelId" type="java.lang.Integer"> <column name="level_id" /> <generator class
LevelTerm.hbm.xml
文件是:
<hibernate-mapping>
<class name="com.entity.LevelTerm" table="level_term" catalog="test">
<id name="levelId" type="java.lang.Integer">
<column name="level_id" />
<generator class="identity" />
</id>
<property name="level" type="int">
<column name="level" not-null="true" />
</property>
<property name="term" type="int">
<column name="term" not-null="true" />
</property>
<property name="session" type="int">
<column name="session" not-null="true" />
</property>
<list name="list_course">
<key column="level_id"/>
<one-to-many column="course_code" class="com.entity.Course"/>
</list>
</class>
</hibernate-mapping>
}
而hibernate.cfg.xml
配置文件是:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.current_session_context_class">thread </property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com.entity/Student.hbm.xml"/>
<mapping resource="com/entity/Address.hbm.xml"/>
<mapping resource="com.entity/Course.hbm.xml"/>
<mapping resource="com.entity/LevelTerm.hbm.xml"/>
</session-factory>
</hibernate-configuration>
org.hibernate.dialogue.mysqldialogue
com.mysql.jdbc.Driver
jdbc:mysql://localhost/test
根
线
创造
我的代码应该在MySQL数据库中创建一个联接表“LEVEL\u TERM\u LIST\u COURSE”。但是没有创建表。不,您的映射不应该生成联接表。它看起来像一个单向的一对多关联,它将由
many
侧的外键管理(示例中的课程)。看一看和相关的解释。这是事实上的SQL
关联方式
此外,如果需要一个联接表,可能需要执行类似于hibernate文档中的操作。基本上,如果在单向一对多
关联中需要一个联接表,则使用多对多
元素,并将unique
属性设置为true
;有效地使关联一对多
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.current_session_context_class">thread </property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com.entity/Student.hbm.xml"/>
<mapping resource="com/entity/Address.hbm.xml"/>
<mapping resource="com.entity/Course.hbm.xml"/>
<mapping resource="com.entity/LevelTerm.hbm.xml"/>
</session-factory>
</hibernate-configuration>