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

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条)

报告相同问题?

悬赏问题

  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路