估计你的Cat类当中有些属性没有设置set方法,如没有setName啊之类的

解决方案 »

  1.   

    有set方法,如果我把数据库里的每个字段都填上值。出来的结果就只正确的
      

  2.   

    那你的映射文件中的字段和你的POJO中的字段一致吗?
      

  3.   

    Cat.hbm.xml:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping>
        <class name="com.xanada.po.Cat" table="CAT">        <id name="id" type="string" unsaved-value="null" >
                <column name="CAT_ID" sql-type="varchar(20)" not-null="true"/>
                <generator class="uuid.hex"/>
            </id>
            <property name="name">
                <column name="NAME" sql-type="varchar(20)" not-null="true"/>
            </property>
            <property name="sex" column="SEX"/>
            <property name="weight" column="WEIGHT"/>
        </class>
    </hibernate-mapping>
    *************
    Cat.java
    package com.xanada.po;public class Cat {
        private String id;
        private String name;
        private char sex;
        private float weight;
        public Cat() {
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public char getSex() {
            return sex;
        }
        public void setSex(char sex) {
            this.sex = sex;
        }
        public float getWeight() {
            return weight;
        }
        public void setWeight(float weight) {
            this.weight = weight;
        }
        public String toString() {
            String strCat = new StringBuffer()
                .append(this.getId()).append(", ")
                .append(this.getName()).append(", ")
                .append(this.getSex()).append(", ")
                .append(this.getWeight())
                .toString();
            return strCat;
        }
    }
      

  4.   

    表结构:如果我把表里的每个字段都填满 程序结果就是正确的
    create table CAT
    (
      CAT_ID VARCHAR2(20) not null,
      NAME   VARCHAR2(20) not null,
      SEX    CHAR(1),
      WEIGHT FLOAT
    )CAT_ID  NAME  SEX WEIGHT
    1       one    F    5
    2       toe    F    4
    3       three  M    8
    4       four        6
      

  5.   

    A PropertyAccessException often occurs when the object being passed to the setter method is of the wrong type. Check your type mappings for the offending property. (To see exactly which property was the problem, you might need to disable the CGLIB reflection optimizer.) However, the most common cause of this problem is that Hibernate attempted to assign null to a property of primitive type.If your object has a primitive-type property mapped to a nullable database column then you will need to use a Hibernate custom type to assign a sensible default (primitive) value for the case of a null column value. A better solution is usually to use a wrapper type for the Java property.这是hibernate官方网站上的FAQ解答,我认为把数据库的null字段改为非null就行了