在此,首先说明一点任何持久性框架都需要解决一个问题,那就是Java实体类的字段一般来说基本上会与数据库表中字段不一致,那么它们是如何解决的呢?咱们以Hibernate和SpringJDBC为例说明一下;
1、Hibernate中一般通过XML映射和注解的方式解决不一致问题,看下面两个简单例子,
注解方式:
@Entity
@Table(name = "ACCOUNT")
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
@Column(name="fld_number")
private String number;
@OneToMany(mappedBy="account")
private Set<Client> clients;
private double balance;
}
XML映射文件配置方式:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.test.domain.Account" table="ACCOUNT" lazy="false">
<id name="id" column="ACCOUNT_ID">
<generator class="native"/>
</id>
<many-to-one name="person" class="org.hibernate.test.domain.Person" cascade="save-update,lock"
column="person_id"
unique="true"
not-null="true"/>
</class>
</hibernate-mapping>
2、Spring