简体   繁体   English

DataNucleus Enhancer,JDO和指定列名称

[英]DataNucleus Enhancer, JDO and Specifying Column Names

I'm working with DataNucleus as part of a Google App Engine project and I'm having a bit of trouble with columns in persistence. 我正在使用DataNucleus作为Google App Engine项目的一部分,但是在持久性方面存在一些麻烦。

@PrimaryKey(column = "user_id")
@Column(name = "user_id")
@Persistent(name = "user_id", column = "user_id", valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key m_id;

@Column(name = "user_name")
@Persistent(name = "user_name", column = "user_name")
private String m_userName;

If you can't tell, I'm trying to name the column something different than the name of the variable because I have two naming conventions (one works better in Java, one works better in SQL). 如果您不知道,我试图用不同于变量名称的名称来命名该列,因为我有两种命名约定(一种在Java中更好,另一种在SQL中更好)。 Anyway, I've tried all variations of those marker annotations but the DataNucleus enhancer refuses to honor any of them, so when I run a query like this: 无论如何,我已经尝试了所有这些标记注释的变体,但是DataNucleus增强器拒绝兑现它们中的任何一个,因此当我运行如下查询时:

Query q = pm.newQuery(User.class,
                      "user_name == _username");

I always get an error like this: 我总是收到这样的错误:

org.datanucleus.store.appengine.FatalNucleusUserException: Unexpected expression type while parsing query. org.datanucleus.store.appengine.FatalNucleusUserException:解析查询时出现意外的表达式类型。 Are you certain that a field named user_name exists on your object? 您确定对象上存在一个名为user_name的字段吗?

Of course, when a run a query like this: 当然,当运行这样的查询时:

Query q = pm.newQuery(User.class,
                      "m_userName == _username");

...everything just works great. ...一切都很好。 So, there would be a field named user_name if any of those annotations were honored, but they're clearly not. 因此,如果这些注释中的任何一个得到了user_name ,都会有一个名为user_name的字段,但显然不是。

SO my question is: Is there any way to decouple the tokens I use in the query from the name of the field? 所以我的问题是:有什么方法可以将查询中使用的令牌与字段名称分离? I'm looking for the ability to change the names of the fields without having to edit the queries by hand. 我正在寻找无需手动编辑查询即可更改字段名称的功能。

NOTE: I would sooner just use my SQL naming conventions in the Java classes than write the hideous amounts of XML by hand, so this has to be done with the annotations. 注意:我将很快在Java类中使用我的SQL命名约定,而不是手工编写大量的XML,因此必须使用批注来完成。

No idea of the talk of SQL, you're using GAE/J hence BigTable and not an RDBMS so SQL just won't work. 不知道SQL的话题,您使用的是GAE / J,因此使用的是BigTable,而不是RDBMS,因此SQL无法使用。 @Column likely does nothing since it is for ORM. @Column可能不执行任何操作,因为它适用于ORM。 Here you're using JDOQLas the query language, so you use field names ... since it is an Object-Oriented query language. 在这里,您使用的是JDOQLas查询语言,因此您使用字段名...,因为它是一种面向对象的查询语言。 This is NOT SQL. 这不是SQL。 You detest "this" ? 您讨厌“这个”吗? JDOQL uses Java syntax, hence "this" makes lots of sense. JDOQL使用Java语法,因此“ this”很有意义。

If you really want to have a type-safe query extension that allows refactoring then QueryDSL provides JDOQL for use with DataNucleus. 如果您确实想要具有允许重构的类型安全的查询扩展,则QueryDSL提供JDOQL以便与DataNucleus一起使用。

PS The DataNucleus enhancer has nothing to do with column names. PS DataNucleus增强器与列名无关。 It simply adds on extra methods for detecting updates to fields, as per the JDO spec. 根据JDO规范,它只是添加了用于检测字段更新的其他方法。

Not 100% sure I get what is your problem. 不能100%确定我能解决您的问题。 If you use m_userName in your query does it get translated as user_name in the SQL query? 如果在查询中使用m_userName ,在SQL查询中是否将其转换为user_name

You express you query according to the java class names and variables and they get translated to work according to the the SQL schema table and column names. 您根据Java类名称和变量表示查询,然后根据SQL模式表和列名称将它们转换为可工作的名称。 That's most of the time what people want. 这是大多数时候人们想要的。

BTW, m_id and m_userName is a terrible naming convention for Java code. BTW, m_idm_userName是Java代码的可怕命名约定。 I would strongly advise you to follow the usual convention . 我强烈建议您遵循通常的惯例

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

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