[英]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
方法不是getMProperty
和setMProperty
。
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.