JavaBean

关于Hibernate的查询从数据库映射到JavaBean

和自甴很熟 提交于 2019-12-10 15:10:05
Hibernate除了HQL外,还支持SQL的查询,API为createSQLQuery(sql),如果数据库使用的是Oracle, 由于数据库表中的列都是大写,所以在从resultset到javabean的时候,需要完全匹配。 一般我们会用DTO或者作为DTO的Entity,无论是采用addEntity(Class class)还是 setResultTransformer(new AliasToBeanResultTransformer (CatDTO.class)) 都会遇到数据库字段到Java的大小写映射的问题,如果数据库字段是小写的id, 数据库里面是大写的ID, 则会遇到 org.hibernate.PropertyNotFoundException : Could not find setter for ID on class com .... 的问题。 通过源码发现,要求java的属性要和数据库的字段名大小写一样,并且全匹配。 这个可以通过模仿Spring的类似查询解决。Spring的NamedJdbcTemplate有如下方法:namedJdbcTemplate.query(sql, params, new BeanPropertyRowMapper(clazz)) 也是通过执行sql并把返回的结果转换成Java的方法,这个就可以忽视数据库字段的大小写问题,

JavaBean PropertyChangeEvent 事件使用

妖精的绣舞 提交于 2019-12-10 15:06:28
In this example we'll listen to bean's property change event. We create a small bean named MyBean , adds attributes and getter/setter. We want to know or to get notified when the bean property name is changed. First we need the add a PropertyChangeSupport field to the bean, with this object we will fire the property change event. When we need to listen for the change we have to create an implementation of a PropertyChangeListener . In this example we'll just use the MyBean class as the listener. The PropertyChangeListener has a method called propertyChange and inside this method we'll