简体   繁体   English

我所有的字段都有一个前缀。 我该如何告诉它休眠?

[英]All my fields have a prefix. How can I tell that to hibernate?

Eg I have: 例如,我有:

  @Column(name = "username")
  private String m_username;

Note that the @Column annotation only affects the database column name. 请注意,@ Column批注仅影响数据库列名。

Hibernate still thinks the name of the property is 'm_username'. Hibernate仍然认为该属性的名称为“ m_username”。

How can I tell Hibernate that the property name is just 'username'? 如何告诉Hibernate属性名称只是“用户名”?

Please tell me there is a way to do this... 请告诉我有一种方法可以做到这一点...

Edit: I removed the @AccessType annotation in my code example, as it is not relevant for this question. 编辑:我在代码示例中删除了@AccessType批注,因为它与该问题无关。

Update: After switching everything to field access, this exception happens: 更新:将所有内容切换到现场访问之后,发生此异常:

org.hibernate.QueryException: could not resolve property: username of: mypackage.model.User

It happens here: 它发生在这里:

Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("username", username));
User result = (User) criteria.uniqueResult();

And the reason is most likely that hibernate only 'knows' of a property called 'm_username', while I think of it and program against a property named 'username'. 原因很可能是仅休眠“知道”名为“ m_username”的属性,而我想到它并针对名为“ username”的属性进行编程。 Also note that my getters/setters are called: "getUsername()" and "setUsername(String value)" (automatically generated). 还要注意,我的getter / setter方法被称为:“ getUsername()”和“ setUsername(String value)”(自动生成)。

why do you use AccessType.PROPERTY ? 为什么使用AccessType.PROPERTY

remove it and it is accessed by 'field'. 删除它,并通过“字段”对其进行访问。

do not mix field and property annotations. 不要混合字段和属性注释。 stick to one or the other. 坚持一个或另一个。

As per my knowledge we can't* . 据我所知,我们不能*。

There is still one issue with the way hibernate looks up the methods for a given property name . hibernate查找given property name的方法的方式仍然存在一个问题。

Suppose you have a property with a name like "mProperty" (first lowercase, second uppercase, rest doesn't matter). 假设您有一个名称类似“ mProperty”的property (第一个小写字母,第二个大写字母,rest都没有关系)。 Not the accessor methods in the source code will be getMProperty and setMProperty . 源代码中的accessor方法不是getMPropertysetMProperty

The way BasicPropertyAccessor.getterMethod is implemented in that way . 以这种方式实现BasicPropertyAccessor.getterMethod方式。

I found one lead here: https://forum.hibernate.org/viewtopic.php?f=1&t=943110 我在这里找到一条线索: https : //forum.hibernate.org/viewtopic.php?f=1&t=943110

But the thread is 7 years old and I don't know how to apply this to annotation based configuration (I don't have an xml configuration file). 但是该线程已有7年历史了,我不知道如何将其应用于基于注释的配置(我没有xml配置文件)。

暂无
暂无

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

相关问题 如果传入请求的字段少于或多于 POJO,我如何告诉 Hibernate 验证器抛出异常 - How can I tell Hibernate Validator to throw exception if the incoming request has less or extra fields than the POJO 如何告诉数据库某些更改是由休眠而不是其他应用程序进行的? - How can I tell my database that some changes are made by hibernate and not by other application? 如何使用HQL / Hibernate验证我的所有表都为空? - How can i verify that all my tables are empty with HQL/Hibernate? 如何确定Hibernate 4实体是代理还是实际实体? - How can I tell if a Hibernate 4 entity is a proxy or the actual entity? 如果我有选择操作,谁能告诉我如何测试我的骆驼路线? - Can anyone tell me how to test my Camel Route if I have a choice operation? 如何直接在我的表中保留 primaryAddress 中的字段? 在我的表中,我没有 primaryAddress 列,但其中的所有字段都是列本身 - How to persist fields in primaryAddress directly in my table ? In my table I have no primaryAddress column but all fields inside it are columns itself 如何告诉Hibernate Search索引FetchType.LAZY字段? - How do I tell Hibernate Search to index FetchType.LAZY fields? 我怎样才能告诉gson Java中将哪些字段添加到json? - How can I tell gson which fields to add to json in Java? 如何在 Hibernate 查询语言中查询所有具有关联实体的实体? - How can I query all entities that have an associated entity in Hibernate Query Language? 如何告诉休眠将调用哪个方法? 我有一堂课,两张桌子 - How to tell hibernate which method will be called? i have one class , 2 tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM