hibernate映射无主键表时会自动生成3个文件分别为:
Table.java 代码下:
public class Table implements java.io.Serializable {
// Fields
private TableId id;
// Constructors
/** default constructor */
public Table() {
}
/** full constructor */
public Table(TableId id) {
this.id = id;
}
// Property accessors
public TableId getId() {
return this.id;
}
public void setId(TableId id) {
this.id = id;
}
TableId.java代码下:
package com.htdz.cust.action;
public class TableId implements java.io.Serializable {
// Fields
private long custid;
private long sale;
private long cons;
private long bala;
private String statperiod;
// Constructors
/** default constructor */
public TableId() {
}
/** full constructor */
public TableId(long custid, long sale, long cons, long bala,
String statperiod) {
this.custid = custid;
this.sale = sale;
this.cons = cons;
this.bala = bala;
this.statperiod = statperiod;
}
// Property accessors
public long getCustid() {
return this.custid;
}
public void setCustid(long custid) {
this.custid = custid;
}
public long getSale() {
return this.sale;
}
public void setSale(long sale) {
this.sale = sale;
}
public long getCons() {
return this.cons;
}
public void setCons(long cons) {
this.cons = cons;
}
public long getBala() {
return this.bala;
}
public void setBala(long bala) {
this.bala = bala;
}
public String getStatperiod() {
return this.statperiod;
}
public void setStatperiod(String statperiod) {
this.statperiod = statperiod;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TableId))
return false;
TableId castOther = (TableId) other;
return (this.getCustid() == castOther.getCustid())
&& (this.getSale() == castOther.getSale())
&& (this.getCons() == castOther.getCons())
&& (this.getBala() == castOther.getBala())
&& ((this.getStatperiod() == castOther.getStatperiod()) || (this
.getStatperiod() != null
&& castOther.getStatperiod() != null && this
.getStatperiod().equals(castOther.getStatperiod())));
}
public int hashCode() {
int result = 17;
result = 37 * result + (int) this.getCustid();
result = 37 * result + (int) this.getSale();
result = 37 * result + (int) this.getCons();
result = 37 * result + (int) this.getBala();
result = 37
* result
+ (getStatperiod() == null ? 0 : this.getStatperiod()
.hashCode());
return result;
}
}
Table.hbm.xml代码下:
<hibernate-mapping>
<class name="com.Cust" table="CUST" schema="MyTables">
<composite-id name="id" class="com.model.TableId">
<key-property name="custid" type="long">
<column name="CUSTID" precision="22" scale="0" />
</key-property>
<key-property name="sale" type="long">
<column name="SALE" precision="22" scale="0" />
</key-property>
<key-property name="cons" type="long">
<column name="CONS" precision="22" scale="0" />
</key-property>
<key-property name="bala" type="long">
<column name="BALA" precision="22" scale="0" />
</key-property>
<key-property name="statperiod" type="string">
<column name="STATPERIOD" length="7" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
—————————————————————————————————————————
刚遇到这种无主键表映射的文件时,可能不太习惯,甚至会手动他把改成有主键表的映射文件形式.这样的话.就会出错啦.就拿最基本的查询来说.如果你改成有主键表映射文件的形式,那么Table中的custid如果有相同值的话,你从Dao中查出来的数据.凡是custid相同的数据.都会显示同一条,这不是我们想要的结果.
下面是一个简单查询案例
Dao.java 代码下:
public List<TableId> findTable(String statperiod) {
List<TableId> tableList = new ArrayList(); // 结果
Session session = null;
try {
// hql r.id.statperiod 先获取对象r的id属性,
// id属性类型为table,所以可以直接r.id.statperiod(TableId中的属性)
String hql = "from Table r where r.id.statperiod = ? order by r.id.custid";
session = this.getSession();
Query query = session.createQuery(hql); // 执行Hql
query.setString(0, statperiod);
List list = query.list();
if (list.size() != 0) {
for (int i = 0; i < list.size(); i++) {
// 从list中迭带出Table(也就是多个TableId对象)
Table table = (Table) list.get(i);
// 然后再将迭带出来的Table付给TableId方便我们在其它地方使用
TableId tableId = table.getId();
// 最后添加到list中
tableList.add(tableId);
}// end for
}// end if
session.flush();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
session.close();
}
return tableList;
}
分享到:
相关推荐
hibernate 无主键表映射资源说明: 1. 简单Demo 2. Demo对应的sql语句 3. 对应博客文档
NULL 博文链接:https://zhangzhi199129.iteye.com/blog/1663919
联合主键用Hibernate注解映射方式主要有三种: 第一、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不包含联合主键...
复合主键映射 <br>通常将复合主键相关属性,单独抽取出来,建立一个独立的类 * 必须实现序列化接口 * 必须实现equals和hashcode方法 采用标签进行映射,其它属性采用正常映射
hibernata 映射主键自动生成,跨数据库时,如何设置,主要讲述在oracle的mapping文件中的设置,以及orcle数据库中的设置
关于hiberante主键的一对一的映射,单向的,实例
hibernate映射文件--主键生成策略[参考].pdf
Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向关联 Hibernate 一对多外键单向关联 Hibernate 一对多连接表单向关联 Hibernate 多对一外键单向关联 Hibernate 多对一连接表单向关联 Hibernate 多对多单向...
Hibernate高级映射的几个单向关系,单向一对一(共享主键方式、唯一外键方式),单向多对一,单向一对多,单向多对多。
Hibernate一对一主键映射实例,里面包含源程序,测试程序,映射说明
这个是针对初学Hibernate一对多主键关联映射的源代码 ,希望对初学者有所帮助,开发环境是MyEclipse7.5和HIbernate3.2
:Hibernate有两种映射实体一对一关联关系的实现方式:共享主键方式和唯一外键方式。 共享主键方式: 所谓共享主键方式就是限制两个数据表的主键使用相同的值
博文链接:https://shaqiang32.iteye.com/blog/201309
hibernate应用[包括示例,映射,主键自增,各种查询操作方式以及配置文档以及Annotation示例]
hibernate主键一对一关联映射代码示例
博文链接:https://llying.iteye.com/blog/220803
博文链接:https://llying.iteye.com/blog/220802