iteye_9642
2010-05-06 21:29
浏览 281
已采纳

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条回答 默认 最新

  • 拽拽的初行者 2010-05-07 12:04
    已采纳

    如果是用Annotation的话 就用 @Transient

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

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)