Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext and generating JPA annotations(How to generate annotation in Xtext)
Xtext and generating JPA annotations [message #1264823] Wed, 05 March 2014 14:32 Go to next message
Mary Se is currently offline Mary SeFriend
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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13275
Registered: July 2009
Senior Member
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
	}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext and generating JPA annotations [message #1265223 is a reply to message #1265179] Wed, 05 March 2014 20:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13275
Registered: July 2009
Senior Member
p.s: the jpa api jar has to be on the classpath of the project containing the dmodel files

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
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 Go to previous messageGo to next message
Mary Se is currently offline Mary SeFriend
Messages: 5
Registered: March 2014
Junior Member
Thanks for your reply Smile

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 wh
ere 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 199 times)
Re: Xtext and generating JPA annotations [message #1265632 is a reply to message #1265600] Thu, 06 March 2014 10:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13275
Registered: July 2009
Senior Member
Hi,

which xtext version do you use
can you try to use a simple java project with jpa api on classpath
can you please share your complete code incl. example project...


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext and generating JPA annotations [message #1265689 is a reply to message #1265632] Thu, 06 March 2014 12:29 Go to previous messageGo to next message
Mary Se is currently offline Mary SeFriend
Messages: 5
Registered: March 2014
Junior Member
Thanks a lot for your quick reply Smile

I am using xtext-2.5.3

Yes, actually I started java project and try to create mydmodel files there, but I got error: Xtext validation

also when I added the second part of your code to the jmvModelInferre class, and start the run-time eclipse I got the error I sent you before.

I attached my complete code and also the screenshot of my java project!
Re: Xtext and generating JPA annotations [message #1265706 is a reply to message #1265689] Thu, 06 March 2014 12:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13275
Registered: July 2009
Senior Member
Hi,

i will have a look tonight


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext and generating JPA annotations [message #1265721 is a reply to message #1265706] Thu, 06 March 2014 13:18 Go to previous messageGo to next message
Mary Se is currently offline Mary SeFriend
Messages: 5
Registered: March 2014
Junior Member
Thanks in advance for your time Smile
Re: Xtext and generating JPA annotations [message #1265742 is a reply to message #1265721] Thu, 06 March 2014 13:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13275
Registered: July 2009
Senior Member
Hi,
it looks like the api changed between 2.5.0 and 2.5.3
i dont know why this is.

it now seems to be

a.explicitValues += t


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext and generating JPA annotations [message #1265774 is a reply to message #1265742] Thu, 06 March 2014 14:40 Go to previous messageGo to next message
Mary Se is currently offline Mary SeFriend
Messages: 5
Registered: March 2014
Junior Member
Hi Christian,

Thanks for your help. now it is working properly Smile
Re: Xtext and generating JPA annotations [message #1408226 is a reply to message #1265774] Thu, 14 August 2014 14:43 Go to previous messageGo to next message
Marko Markovic is currently offline Marko MarkovicFriend
Messages: 35
Registered: August 2014
Member
Hi,

I have similar issue (printscreen is in attachment).
I wrote next code:

acceptor.accept(entity.toClass(entity.fullyQualifiedName)).initializeLater [

annotations += entity.toAnnotation(javax.ejb.Stateful)
annotations += entity.toAnnotation(javax.ejb.Remote)

I would like to have java class like next code:

@Stateful
@Remote(MyClass2.class)
public class MyClass {
...
}

I have jboss-ejb-api_3.2_spec-1.0.0.Final.jar on classpath in my project (also, you can see it in printscreen)

Mary, how did you solve your problem?

Thanks in advance,

Marko
Re: Xtext and generating JPA annotations [message #1409214 is a reply to message #1408226] Sun, 17 August 2014 12:35 Go to previous messageGo to next message
Marko Markovic is currently offline Marko MarkovicFriend
Messages: 35
Registered: August 2014
Member
I solved my issue, I wrote javax.ejb.Stateful with quotation marks as "javax.ejb.Stateful" but I now have another issue: next code isn't work, annotations += entity.toAnnotation("javax.ejb.Remote", "value" -> xjaf.server.agm.AgentI). It should look like: @Remote(AgentI.class).
Can someone help me?

Thanks in advance,
Marko
Re: Xtext and generating JPA annotations [message #1409220 is a reply to message #1409214] Sun, 17 August 2014 13:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13275
Registered: July 2009
Senior Member
have a look at JvmTypeAnnotationValue



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext and generating JPA annotations [message #1411923 is a reply to message #1409220] Sun, 24 August 2014 21:54 Go to previous message
Marko Markovic is currently offline Marko MarkovicFriend
Messages: 35
Registered: August 2014
Member
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
Previous Topic:[SOLVED] Serializer Enum
Next Topic:How to test NatTable using swtbot?
Goto Forum:
  


Current Time: Thu Dec 12 21:25:46 GMT 2019

Powered by FUDForum. Page generated in 0.02488 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top