Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Combining Xtext with a MySQL database(Technologies for an instance inside a database that is read/updated by a webapp)
Combining Xtext with a MySQL database [message #1750968] Tue, 03 January 2017 08:16 Go to next message
Jahic Benjamin is currently offline Jahic BenjaminFriend
Messages: 10
Registered: October 2015
Junior Member
Hi all,

I'm currently working on a project, where I am using Xtext and a web application.

For the web application I am using a MVC structure and it is mainly based on the Spring Framework. For storing/reading data to/from the database, I am using Spring data (javax.persistence annotations). I wanted to use something similar inside the Xtext project in order to create the database model.

The problem is that I am working with a "Model-based approach" and I would like to configure the Content of the Web Application using my DSL. After I have specified my configuration using my small DSL, I would like to store some data to a MYSQL database. The database model should reflect more or less the Ecore Model. After that the web-application is updated according to the content stored inside the DSL. I do not know how to create a persistence database according to the Ecore model, so when I change my grammar, the database model should be updated too.

I was looking at the following posts :

QUESTION 1 : Since EMFStore, Teneo and CDO seems to be "pretty old" and not updated, are there any updated solutions for storing data to the database?

I was also trying to add the javax.persistence annotations like "@Entity", "@Id", "@OneToMany",... to the src-gen by using the post processor. Unfortunately, i am not able to create it?

QUESTION 2 : Is there a possibility to add these annotations to the src-gen in order to guarantee a database structure that is based on the model created with the grammar?

Since I am working with Model-Based approach, I would like to use the database model and the database services (storage, getter) of the Xtext project, inside the Webapp, without duplicating the "database model" in order to improve the maintainability.

QUESTION 3 : Do you have any suggestions, how to create the database based on the grammar and the generated ecore model ? Or any other suggestion on how to combine the web app with a Xtext project?

Thanks for any help and I wish you all a happy new year

Kind Regards,
Re: Combining Xtext with a MySQL database [message #1751243 is a reply to message #1750968] Fri, 06 January 2017 08:20 Go to previous message
Jahic Benjamin is currently offline Jahic BenjaminFriend
Messages: 10
Registered: October 2015
Junior Member

I may propose a solution to people who are interested in. Unfortunately, it is a manual implementation of the database. I have created the database using jdbc (java.sql classes), by implementing manually the SQL command to create all tables and references in between these tables. The Ecore Model represents the database.

Here is my implementation to make the Post Processor run, I guess it is not obvious. So it might be of interest to post it... Smile

I have modified the standard MWE2 Workflow to the following one :
The only problem is that the MWE2 Workflow causes a small error in the in src-gen. A small cast is missing in the bindIGenerator function. Smile (Quickfix will do it)

module lu.uni.lassy.messep.mespro.GenerateMyDsl

import org.eclipse.emf.mwe.utils.*
import org.eclipse.xtext.generator.*
import org.eclipse.xtext.ui.generator.*

var grammarURI = "classpath:****ENTER_CLASSPATH_2_XTEXT-FILE******/MyDsl.xtext"
var fileExtensions = "************ENTER THE FILE EXTENSION****************"
var projectName = "************ENTER THE PROJECT NAME***********"
var runtimeProject = "../${projectName}"
var generateXtendStub = false
var encoding = "UTF-8"

Workflow {
    bean = StandaloneSetup {
    	scanClassPath = true
    	platformUri = "${runtimeProject}/.."
    	// The following two lines can be removed, if Xbase is not used.
    	registerGeneratedEPackage = "org.eclipse.xtext.xbase.XbasePackage"
    	registerGenModelFile = "platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel"
    component = DirectoryCleaner {
    	directory = "${runtimeProject}/src-gen"
    component = DirectoryCleaner {
    	directory = "${runtimeProject}/model/generated"
    component = DirectoryCleaner {
    	directory = "${runtimeProject}.ui/src-gen"
    component = ExtendedGenerator {
    	pathRtProject = runtimeProject
    	pathUiProject = "${runtimeProject}.ui"
    	//pathTestProject = "${runtimeProject}.tests"
    	projectNameRt = projectName
    	projectNameUi = "${projectName}.ui"
    	encoding = encoding
    	language = auto-inject {
    		uri = grammarURI
    		// Java API to access grammar elements (required by several other fragments)
    		fragment = grammarAccess.GrammarAccessFragment auto-inject {}
    		// generates Java API for the generated EPackages
    		fragment = ecore.EMFGeneratorFragment auto-inject {}
    		// the old serialization component
    		// fragment = parseTreeConstructor.ParseTreeConstructorFragment auto-inject {}    
    		// serializer 2.0
    		fragment = serializer.SerializerFragment auto-inject {
    			generateStub = false
    		// a custom ResourceFactory for use with EMF
    		fragment = resourceFactory.ResourceFactoryFragment auto-inject {}
    		// The antlr parser generator fragment.
    		fragment = parser.antlr.XtextAntlrGeneratorFragment auto-inject {
   				options = {
    		//      backtrack = true
    				classSplitting = true
    				fieldsPerClass = "100"
    				methodsPerClass = "100"
    		// generates a more lightweight Antlr parser and lexer tailored for content assist
    		fragment = parser.antlr.XtextAntlrUiGeneratorFragment auto-inject {
				options = {
    		//      backtrack = true
    				classSplitting = true
    				fieldsPerClass = "100"
    				methodsPerClass = "100"
    		// java-based API for validation
            fragment = validation.JavaValidatorFragment {
                composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
                composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
    		// old scoping and exporting API
    		// fragment = scoping.ImportURIScopingFragment auto-inject {}
    		// fragment = exporting.SimpleNamesFragment auto-inject {}
    		// scoping and exporting API
    		fragment = scoping.ImportNamespacesScopingFragment auto-inject {}
    		fragment = exporting.QualifiedNamesFragment auto-inject {}
    		fragment = builder.BuilderIntegrationFragment auto-inject {}
    		// generator API
    		fragment = generator.GeneratorFragment auto-inject {}
    		// formatter API
    		fragment = formatting.FormatterFragment auto-inject {}
    		// labeling API
    		fragment = labeling.LabelProviderFragment auto-inject {}
    		// outline API
    		fragment = outline.OutlineTreeProviderFragment auto-inject {}
    		fragment = outline.QuickOutlineFragment auto-inject {}
    		// quickfix API
    		fragment = quickfix.QuickfixProviderFragment auto-inject {}
    		// NEW!!! Xtend-based content assist API
    		// fragment = contentAssist.ContentAssistFragment auto-inject {}
    		// ORIGINAL!!! Java-based content assist API
            fragment = contentAssist.JavaBasedContentAssistFragment {}
    		// generates junit test support classes into Generator#pathTestProject
    		fragment = junit.Junit4Fragment auto-inject {}
    		// rename refactoring
    		fragment = refactoring.RefactorElementNameFragment auto-inject {}
    		// provides the necessary bindings for java types integration
    		fragment = types.TypesGeneratorFragment auto-inject {}
    		// generates the required bindings only if the grammar inherits from Xbase
    		fragment = xbase.XbaseGeneratorFragment auto-inject {}
    		// generates the required bindings only if the grammar inherits from Xtype
    		fragment = xbase.XtypeGeneratorFragment auto-inject {}
    		// provides a preference page for template proposals
    		fragment = templates.CodetemplatesGeneratorFragment auto-inject {}
    		// provides a compare view
    		fragment = compare.CompareFragment auto-inject {}

A ExtendedGenerator class that activates the post processor.

package com.test.mydsl;

import org.eclipse.xtext.XtextRuntimeModule;
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.generator.Generator;
import org.eclipse.xtext.xtext.ecoreInference.IXtext2EcorePostProcessor;


public class ExtendedGenerator extends Generator{
	  public ExtendedGenerator() {
		    new XtextStandaloneSetupExtension().createInjectorAndDoEMFRegistration();
	private static class XtextStandaloneSetupExtension extends XtextStandaloneSetup {
	    @Override public Injector createInjector() {
	      return Guice.createInjector(new XtextRuntimeModuleExtension());
	  private static class XtextRuntimeModuleExtension extends XtextRuntimeModule {
	    @Override public void configureIXtext2EcorePostProcessor(Binder binder) {

I have created a PostProcessor class that gives me the EMF Model.
public class ctPostProcessor  implements IXtext2EcorePostProcessor {

	public void process(GeneratedMetamodel metamodel) {
		// TODO Auto-generated method stub
	public static void process(EPackage p) {
                  //MySQL Database Implementation using jdbc

Once the implementation is done, you click on generateXtextArtifacts and the "process" function. Once that is done,the database will ready toooo be useeed.. Smile

I hope this might help somebody Smile

[Updated on: Fri, 06 January 2017 08:25]

Report message to a moderator

Previous Topic:Xtext Web Editor
Next Topic:function call operator?
Goto Forum:

Current Time: Fri Sep 29 04:54:34 GMT 2023

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

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

Back to the top