senjsoo 2009-11-13 00:27
浏览 282
已采纳

用hibernateTool生成pojo类时,多了一个 表名Id.java 的pojo,为什么

比如数据库中表为table.用hibernateTool生成pojo和hbm时,会产生一个 TableId.java和一个Table.java,为什么,
这时候如何插入数据,

Table.java:

public class Table implements java.io.Serializable {

private TableId id;

public Table() {
}
   ....

}

TableId.java中有数据库中表table 的实际属性,这时怎么操作,或者哪位提供个文档,我自己去看也行.
[b]问题补充:[/b]
Table里只有一个Id属性.
就算new一个,也一样,还是只有一个setId的方法,没有set其它属性的方法.
[b]问题补充:[/b]
首先对表示感谢.然后贴一下数据库设置.

bz_id char(1)
bz_name varchar(20)
idx smallint

CREATE TABLE [dbo].BZ NULL CONSTRAINT [DF_BZ_bz_id] DEFAULT (''),
[bz_name] varchar NULL CONSTRAINT [DF_BZ_bz_name] DEFAULT (''),
[idx] [smallint] NULL CONSTRAINT [DF_BZ_idx] DEFAULT (0)
) ON [PRIMARY]

里面没有设置主键.而且id和idx都允许空的,(因为业务要求不同,所以有两个ID.)
[b]问题补充:[/b]
下面是Bz.hbm.xml
[code="java"]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">















[/code]
[b]问题补充:[/b]
Bz.java
[code="java"]
package org.gaodi.bean;

// Generated 2009-11-11 23:23:08 by Hibernate Tools 3.2.4.GA

/**

  • Bz generated by hbm2java
    */
    public class Bz implements java.io.Serializable {

    private BzId id;

    public Bz() {
    }

    public Bz(BzId id) {
    this.id = id;
    }

    public BzId getId() {
    return this.id;
    }

    public void setId(BzId id) {
    this.id = id;
    }

}
[/code]

BzId.java
[code="java"]
package org.gaodi.bean;

// Generated 2009-11-11 23:23:08 by Hibernate Tools 3.2.4.GA

/**

  • BzId generated by hbm2java
    */
    public class BzId implements java.io.Serializable {

    private Character bzId;
    private String bzName;
    private Short idx;

    public BzId() {
    }

    public BzId(Character bzId, String bzName, Short idx) {
    this.bzId = bzId;
    this.bzName = bzName;
    this.idx = idx;
    }

    public Character getBzId() {
    return this.bzId;
    }

    public void setBzId(Character bzId) {
    this.bzId = bzId;
    }

    public String getBzName() {
    return this.bzName;
    }

    public void setBzName(String bzName) {
    this.bzName = bzName;
    }

    public Short getIdx() {
    return this.idx;
    }

    public void setIdx(Short idx) {
    this.idx = idx;
    }

    public boolean equals(Object other) {
    if ((this == other))
    return true;
    if ((other == null))
    return false;
    if (!(other instanceof BzId))
    return false;
    BzId castOther = (BzId) other;

    return ((this.getBzId() == castOther.getBzId()) || (this.getBzId() != null
            && castOther.getBzId() != null && this.getBzId().equals(
            castOther.getBzId())))
            && ((this.getBzName() == castOther.getBzName()) || (this
                    .getBzName() != null
                    && castOther.getBzName() != null && this.getBzName()
                    .equals(castOther.getBzName())))
            && ((this.getIdx() == castOther.getIdx()) || (this.getIdx() != null
                    && castOther.getIdx() != null && this.getIdx().equals(
                    castOther.getIdx())));
    

    }

    public int hashCode() {
    int result = 17;

    result = 37 * result
            + (getBzId() == null ? 0 : this.getBzId().hashCode());
    result = 37 * result
            + (getBzName() == null ? 0 : this.getBzName().hashCode());
    result = 37 * result
            + (getIdx() == null ? 0 : this.getIdx().hashCode());
    return result;
    

    }

}

[/code]

  • 写回答

5条回答 默认 最新

  • weixin_42519046 2009-11-13 11:08
    关注

    我知道你那个怎么回事了 你数据库表里的主键应该是两个以上吧?
    先new 一个TableId,然后将TableId里的属性都set了,然后再建立一个table 执行UPDATE

    TableId tableid=new TableId();
    tableid.setXXX();
    tableid.setXXX();
    ....
    [color=red]Table t=new Table();
    t.setTableId(tableid) ;[/color]

    Session session=this.getSession();
    Transaction tx = session.beginTransaction();
    [color=red]session.update(t); [/color]
    tx.commit();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题