简体   繁体   English

Java Hibernate字段访问属性或基于属性的访问

[英]Java Hibernate field access property or property based access

i am developing some improvements over a legacy system have some quite time 我正在对遗留系统进行一些改进,但需要一些时间

i have a class like this 我有这样的课

class MyPersistentClazz
{
      private String aTPlace;
      public void setATPlace(.......){......}//yes mistyping    
      @Column(name="atPlaceOrder")
      public String getATPlace(){return aTPlace;}
}

they usually load this class using this methods 他们通常使用这种方法加载此类

final MyPersistentClazz clazz = (MyPersistentClazz)session.createCriteria(MyPersistentClazz.class).add(idEq(id)).uniqueResult();

and using load and get methods and works OK. 并使用loadget方法,效果很好。

but the problem arise when i use projections. 但是当我使用投影时会出现问题。

final Projection p=Projections.projectionList().add(Projections.property("d.aTPlace"),"aTPlace");

throws

Exception in thread "main" org.hibernate.QueryException: could not resolve property:

my question is... 我的问题是

when using projections i think Hibernate is calling the setter of each property is this assertion OK? 当我使用投影时,我认为Hibernate正在调用每个属性的setter,这个断言可以吗?

when using criteria.uniqueResult or load or get Hibernate use individual field property access? 使用criteria.uniqueResult或加载或获取休眠时使用单个字段属性访问?

or why works with some and not work with others with the same setter? 或为什么与某些人一起工作而不与同一设置员一起工作?

we are using only annotations not XML. 我们仅使用注释而不使用XML。

thanks a lot. 非常感谢。

How Hibernate works with your bean depends on how you annotated it. Hibernate如何与您的bean一起使用取决于您对其进行注释的方式。 If you annotate instance variables then Hibernate will use direct injection and bypass your Set methods. 如果您注释实例变量,则Hibernate将使用直接注入并绕过Set方法。 Otherwise, it will use your Set methods. 否则,它将使用您的Set方法。

Could it be that it is incorrectly converting your property name to a Set method name? 可能是因为您将属性名称错误地转换为Set方法名称吗? Try changing the property name to something simpler (without that series of capital letters), and ensure that the case of the property in your projection is correct. 尝试将属性名称更改为更简单的名称(不要使用该系列的大写字母),并确保投影中属性的大小写正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM