Hibernate实体类注解中忽略某些字段的映射

 使用注解

@Transient在该字段上

例如:

 @Transient

   private int  name;

上面的是将注解放在属性上,有的博客说也可以放在 set/get方法上,

我的是放在后者上 大家可以自己测试下,也可以想下为什么两者都可以

可以在下面评论让更多的人知道

我的是要给一个实体类添加一个List集合属性!

private List<Reverts> childList = new ArrayList<Reverts>();

写完后get/set方法一写,启动就报错:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: T_REVERTS, for columns: [org.hibernate.mapping.Column(childList)]

解决办法:在属性的get方法上加上一段注解标识它是临时属性,不是数据库字段就OK

@Transient 
    public List<Reverts> getChildList() {
        return childList;
    }
    public void setChildList(List<Reverts> childList) {
        this.childList = childList;
    }

记得导入的是:import javax.persistence.Transient;
 

最后切记导入的包,我当时就没注意

猜你喜欢

转载自blog.csdn.net/Architect_CSDN/article/details/90785895