iteye_9642
iteye_9642
2010-05-06 21:29

pojo中的域并不在数据库中存在,hibernate如何定义

已采纳

hibernate 映射的class的变量可以定义多于数据库中的域么?pojo中的域并不在数据库中存在,hibernate如何定义

即类似 grails中class定义中的transients属性

即:pojo中的值并不在数据库中存在

如中间变量或数据

当然,多添加个BO来做没问题,再赋值,再保存。感觉没必要而已。就如grails中,直接赋予transients属性就很简单


搜索了下,貌似无解

http://jira.codehaus.org/browse/GRAILS-4978

与我提的问题有点反方向,但解答有关:

In effect, transient properties are not part of Hibernate (annotation) mapping meta data and this appears to be very reasonable, as @Transient states just that Hibernate should completely ignore a particular property.
Within org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateDomainClass, Grails then uses these meta data to construct the properties of the persistent class. This will result in transient properties missing from the properties collection of a persistent class

For your example, transientField will not be a member of myDomainInstance.properties but can still be accessed via myDomainInstance.transientField.
As the scaffolding logic uses the properties from the persistent class, it will just not see any transient property.
A fix might require GrailsHibernateDomainClass to introspect the persistent class on its own and to merge those properties with the properties from the meta class - provided there is a way to add properties that are not mapped by Hibernate.
A work-around might be attempted by adding missing properties within application code, but I suspect it may not be a task for the faint of heart


嘿嘿,貌似扩展个类,再upcasting就行了,试试看。----我自言自语ing

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • xiaolongfeixiang 拽拽的初行者 11年前

    如果是用Annotation的话 就用 @Transient

    如果是用XML的话,就不再XML中配置

    点赞 评论 复制链接分享
  • xiaolongfeixiang 拽拽的初行者 11年前

    [quote]xiaolongfeixiang 写道
    如果是用Annotation的话 就用 @Transient

    是在class的定义中?还是在hbm配置中? [/quote]

    在POJO中定义。

    XML的配置 优先级高于 Annotation

    点赞 评论 复制链接分享

相关推荐