请问我想在数据库里的tb_smallType表里插入一行数据,插入的数据因为有用到主外键关系所以就在hbm.xm里面配置了
一个多对一的关系,在控制台上照常显示  Hibernate: insert into tb_smallType (creaTime, smallName, bigId) values (?, ?, ?)
可是我总是往数据里面查数据就是查不到有数据插进去。不知道为什么请问这种问题该怎么解决?注:而我其他表没配主外键关系的话就能插进去,请问和这个主外键关系有关吗?配置如下所示:dao包
public void insertSmall(SmallType smallType) {
// TODO Auto-generated method stub

System.out.println("111"+smallType.getBigId());
smallType.setCreaTime(new Date());

Transaction tx = null;
Session session = null;
try {
session = getSession();
tx = session.beginTransaction();
session.save(smallType);
tx.commit();

}  catch (Exception e) {
if(tx != null) tx.rollback();
// e.printStackTrace();
} finally {
session.close();
}


}
SmallType类:
public class SmallType
     {
  private Integer bigId=Integer.valueOf("-1");//商品大类别表的外键
  private Date creaTime;//创建时间
  private Integer id=Integer.valueOf("-1");//数据库流水号
  private String smallName="";//商品小类别信息
  private BigType bigType;
public BigType getBigType() {
return bigType;
}public void setBigType(BigType bigType) {
this.bigType = bigType;
}public Integer getBigId() {
    return bigId;
  }  public void setBigId(Integer bigId) {
    this.bigId = bigId;
  }  public void setSmallName(String smallName) {
    this.smallName = smallName;
  }  public void setId(Integer id) {
    this.id = id;
  }  public Date getCreaTime() {
return creaTime;
}public void setCreaTime(Date creaTime) {
this.creaTime = creaTime;
}public Integer getId() {
    return id;
  }  public String getSmallName() {
    return smallName;
  }
}SmallType.hbm.xml
<hibernate-mapping>
    <class name="com.org.yjh.model.SmallType" table="tb_smallType">
        <id name="id" type="java.lang.Integer">   <!-- <id表示这个name="id"是主键,其他有property就不是主键 -->
            <column name="id" />
            <generator class="native" />          <!-- native意思本国的,本土的 ,generator生产者意思    native设置主键-->
        </id>
        <property name="creaTime" type="java.util.Date">
            <column name="creaTime" />
        </property>
        <property name="smallName" type="java.lang.String">
            <column name="smallName" />
        </property>
        
          <many-to-one name="bigType" 
                     lazy="false" 
                     column="bigId" 
                     class="com.org.yjh.model.BigType"
                     cascade="all"
                     outer-join="true"/>      


        
    </class>
</hibernate-mapping>

解决方案 »

  1.   

    <many-to-one name="bigType" 
                        lazy="false" 
                        column="bigId" 
                        class="com.org.yjh.model.BigType" 
                        cascade="all" 
                        outer-join="true"/>    对这个配置的outer-join="true"不太理解,好像根本就没有这么配的。
      

  2.   

    你inverse是不是两边都取的默认值啊
      

  3.   

    public void insertSmall(SmallType smallType) {
    // TODO Auto-generated method stub
        System.out.println("smalltype"+smallType.getBigId());
    System.out.println("111"+smallType.getBigId());
    smallType.setCreaTime(new Date());
    smallType.setBigId(smallType.getBigId());

    Transaction tx = null;
    Session session = null;
    try {
    session = getSession();
    tx = session.beginTransaction();
     BigType bigType = (BigType)session.load(BigType.class,smallType.getBigId());//////////////加入这两句话
                 smallType.setBigType(bigType);////////////
    session.save(smallType);
    tx.commit();

    }  catch (Exception e) {
    if(tx != null) tx.rollback();
    e.printStackTrace();
    } finally {
    session.close();
    }

    解决了,用级联就可以实现了关键代码:
     BigType bigType = (BigType)session.load(BigType.class,smallType.getBigId());//////////////加入这两句话
                 smallType.setBigType(bigType);////////////
      

  4.   

    不是因为你加入了那两句话的关系吧?只是因为你这次为smallType设置了ID。