Xtext and generating JPA annotations [message #1264823] |
Wed, 05 March 2014 09:32  |
Eclipse User |
|
|
|
Hi,
I want to change domainmodel example on the xtext website to generate annotations same as the example below, regarding to use the generated entity classes in the JPA application.
@Entity
@Table(name = 'USERS')
@NamedQuery(name='User.findUserByEmail', query='select u from User u where u.email = :email')
does anyone know?
Thanks in advance,
Mary
[Updated on: Wed, 05 March 2014 13:18] by Moderator
|
|
|
Re: Xtext and generating JPA annotations [message #1265179 is a reply to message #1264823] |
Wed, 05 March 2014 14:53   |
Eclipse User |
|
|
|
Hi,
i am not sure what your specific question is...
i asume you are using the domain model example that leverages xbase.
then you can simply* extend the jvmmodelinferrer like...
( i dont know your logic how to get the table name and finder....)
unfortunately the api for annotation values seem to suck a bit ....
def dispatch infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
acceptor.accept(
entity.toClass( entity.fullyQualifiedName )
).initializeLater [
...
annotations += entity.toAnnotation("javax.persistence.Entity")
annotations += entity.toAnnotation("javax.persistence.Table", "name" -> entity.name.toUpperCase)
annotations += entity.toAnnotation("javax.persistence.NamedQuery", "name" -> "User.findUserByEmail","query"->'select u from User u where u.email = :email')
]
}
@Inject TypesFactory typesFactory
def JvmAnnotationReference toAnnotation(EObject ctx, String typeName, Pair<String,String>... values) {
val a = ctx.toAnnotation(typeName)
for (v : values) {
val t = typesFactory.createJvmStringAnnotationValue
t.values.add(v.value)
t.operation = a.annotation.members.filter(JvmOperation).filter[simpleName==v.key].head
a.values += t
}
a
}
|
|
|
|
Re: Xtext and generating JPA annotations [message #1265600 is a reply to message #1265223] |
Thu, 06 March 2014 04:53   |
Eclipse User |
|
|
|
Thanks for your reply 
Actually when I added this part of code:
annotations += entity.toAnnotation("javax.persistence.Entity")
annotations += entity.toAnnotation("javax.persistence.Table", "name" -> entity.name.toUpperCase)
annotations += entity.toAnnotation("javax.persistence.NamedQuery", "name" -> "User.findUserByEmail","query"->'select u from User u where u.em[/size]ail = :email')[/size]
At my eclipse run-time eclipse I could just generate:
@Entity
@Table
but no @Table(name = 'USERS') & @NamedQuery(name='User.findUserByEmail', query='select u from User u where u.email = :email'!!!
Than added the second part of your code, I got an error! I think there is a problem with the t.operation part!
@Inject TypesFactory typesFactory
def JvmAnnotationReference toAnnotation(EObject ctx, String typeName, Pair<String,String>... values) {
val a = ctx.toAnnotation(typeName)
for (v : values) {
val t = typesFactory.createJvmStringAnnotationValue
t.values.add(v.value)
t.operation = a.annotation.members.filter(JvmOperation).filter[simpleName==v.key].head
a.values += t
}
a
}
I attached the error I got, Also I should mention that instead of creating the java project at run-time eclipse, I have EJB project!
Attachment: error
(Size: 28.42KB, Downloaded 244 times)
|
|
|
|
|
|
|
|
|
|
|
|
Re: Xtext and generating JPA annotations [message #1411923 is a reply to message #1409220] |
Sun, 24 August 2014 17:54  |
Eclipse User |
|
|
|
Thank you Christian for your suggestion. I solved my problem. Solution is:
@Inject TypeReferences typeReferences;
@Inject TypesFactory typesFactory
def JvmAnnotationReference toAnnotation(/* @Nullable */ EObject sourceElement, /* @Nullable */ String annotationTypeName, /* @Nullable */ Class<?> value) {
val result = typesFactory.createJvmAnnotationReference() as JvmAnnotationReference;
val jvmType = typeReferences.findDeclaredType(annotationTypeName, sourceElement) as JvmAnnotationType;
if (jvmType == null) {
throw new IllegalArgumentException("The type "+annotationTypeName +" is not on the classpath.");
}
if (!(jvmType instanceof JvmAnnotationType)) {
throw new IllegalArgumentException("The given class " + annotationTypeName + " is not an annotation type.");
}
result.setAnnotation(jvmType);
val type = typeReferences.findDeclaredType(value, sourceElement)
if (value != null) {
val annotationValue = typesFactory.createJvmTypeAnnotationValue() as JvmTypeAnnotationValue
annotationValue.getValues().add (typeReferences.createTypeRef(type));
result.getExplicitValues().add(annotationValue);
}
result;
}
...
annotations += entity.toAnnotation("javax.ejb.Remote", AgentI)
Regards
|
|
|
Powered by
FUDForum. Page generated in 0.14980 seconds