Xtext and generating JPA annotations [message #1264823] |
Wed, 05 March 2014 14:32 |
Mary Se Messages: 5 Registered: March 2014 |
Junior Member |
|
|
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 18:18] by Moderator Report message to a moderator
|
|
|
Re: Xtext and generating JPA annotations [message #1265179 is a reply to message #1264823] |
Wed, 05 March 2014 19:53 |
|
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
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: Xtext and generating JPA annotations [message #1265600 is a reply to message #1265223] |
Thu, 06 March 2014 09:53 |
Mary Se Messages: 5 Registered: March 2014 |
Junior Member |
|
|
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 228 times)
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.26103 seconds