hibernate详解(三)----->>组件映射(二)之复合主键
?
?? 3、组件类作为持久化类的对象标识符属性
???? 这种情况主要是针对已经数据库表建模时,由于数据库表中采用联合自然主键(复合自然主键),为了完成这种映射需求,Hibernate使用组件类把它映射成持久化类的对象标识符。
???? Hibernate要求,作为复合对象标识符类的UserOid类必须实现Java.io.Serializable,且要用作为复合标识符的属性重写hashCode()方法和equlas()方法。
?
??? User.java
?
?
?
?
??? hibernate.cfg.xml
?
?
?
>?? ??<hibernate-configuration>?? ????<session-factory>?? ???????? ??????????<!--?数据库方言设置?-->?? ????????<property?name="hibernate.dialect">?? ????????????org.hibernate.dialect.MySQLInnoDBDialect ??????????</property>?? ???????? ??????????<!--?数据库连接参数设置?-->?? ????????<property?name="hibernate.connection.driver_class">?? ????????????com.mysql.jdbc.Driver ??????????</property>?? ????????<property? ?? ??name="hibernate.connection.url">jdbc:mysql:///hibernate</property>?? ????????<property?name="hibernate.connection.username">root</property>?? ????????<property?name="hibernate.connection.password">123</property>?? ???? ??????????<!--实际操作数据库时是否显示SQL?-->?? ????????<!--? ?????????<property?name="hibernate.show_sql">true</property>-->??????????<property?name="hibernate.format_sql">true</property>?? ???????? ?????????? ??????????<!--将数据库schema的DDL导出到数据库?-->?? ????????<property?name="hibernate.hbm2ddl.auto">update</property>?? ??????????<!--?以下定义实体类与数据库表的映像文件?-->?? ????????<mapping?resource="com/zxf/domain/User.hbm.xml"?/>?? ????</session-factory>?? </hibernate-configuration>??