Implementing without grails, and if it doesn't work on tomcat what does it work on?

39 views
Skip to first unread message

caden whitaker

unread,
Oct 19, 2010, 11:02:36 AM10/19/10
to Grails Dynamic Domain Class Plugin
Curious to know how far out you are thinking before it works in
Tomcat, I've been looking into the same issue, been posting questions
all over the net to find something similar to what you are looking.
Below is a trace of all the places I've been posting to:

I'm going to repost my code here at the bottom, But here is the core
question: I'm investigating the concept of Groovy classes as
uncompiled files that are compiled on startup. I'd like these classes
to have the benefits of an ORM. Is this possible? How can I go about
doing this? I'd prefer to not have this be tied to a specific web
architecture just yet. Can I do this with GORM? Any help would be
appreciated. Here is my code example:

I've got a file on my machine called User.groovy
package com.test;


package com.test;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
import com.test.DataUtils;

@Entity
@Table(name = "user")
public class User {

public test() {
def manager = DataUtils.getInstance().createEntityManager()
manager.getTransaction().begin()
manager.persist new User(name: "Peter")
manager.getTransaction().commit()
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false)
Long id;

@Basic
@Column(name = "Name", nullable = true, unique = false)
String name;
}


Then I created a JSP to read in that file and compile it dynamically:

File file = new File("C:\\User.groovy");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;

reader = new BufferedReader(new FileReader(file));
String text = null;

// repeat until all lines is read
while ((text = reader.readLine()) != null)
{
contents.append(text)
.append(System.getProperty(
"line.separator"));
}

// show file contents here
System.out.println(contents.toString());

GroovyClassLoader gcl = new GroovyClassLoader();
Object[] args = {};

((GroovyObject)gcl.parseClass(contents.toString()).newInstance()).invokeMethod("test",
args);

Do you think this would be easy to implement without running grails,
just GORM? I’ve been looking for an answer to this question for the
last week and I’m just going nowhere. Refrenced links:

http://groovy.329449.n5.nabble.com/Hibernate-Dynamic-Groovy-Class-Is-this-even-possible-td3215787.html#a3215787

http://grails.1312388.n4.nabble.com/GORM-Dynamic-Groovy-Class-Is-this-even-possible-td3000509.html#a3000509

http://www.coderanch.com/t/513878/ORM/java/Hibernate-Dynamic-Groovy-Class-even

Chee Kin Lim

unread,
Oct 20, 2010, 1:42:32 AM10/20/10
to grails-dynamic-do...@googlegroups.com
Hi there,

Thanks for writing. Please see my reply below.

Regards,
Chee Kin


On Tue, Oct 19, 2010 at 11:02 PM, caden whitaker <caden.w...@gmail.com> wrote:
Curious to know how far out you are thinking before it works in
Tomcat, I've been looking into the same issue, been posting questions
all over the net to find something similar to what you are looking.
Below is a trace of all the places I've been posting to:
 
I not sure the time frame as I have no idea how to solve the issue. It is related to classloading and may need to touch the core of grails.
 

I'm going to repost my code here at the bottom, But here is the core
question: I'm investigating the concept of Groovy classes as
uncompiled files that are compiled on startup. I'd like these classes
to have the benefits of an ORM. Is this possible? How can I go about
doing this? I'd prefer to not have this be tied to a specific web
architecture just yet.

I not sure, I think you will need to deal with classloading and Groovy compiler.
 
Can  I do this with GORM? Any help would be
appreciated.

Have you use GORM outside of Grails? I never.
 
As you are using JPA, I think you not only need to dynamically compile the class you also need to register it to the JPA provider such as Hibernate. I think the post at http://www.jroller.com/melix/entry/dynamic_database_models_with_groovy have the solution you are looking for.


limcheekin

unread,
Oct 30, 2010, 11:12:25 AM10/30/10
to Grails Dynamic Domain Class Plugin
Hi Caden,

May I know you found the solution?

By the way, will you consider other JPA 2.0 implementation instead of
Hibernate such as EclipseLink at http://www.eclipse.org/eclipselink/?

FYI, If I am unable to make Hibernate to support dynamic entity (as it
was not design to do so) in production, I will switch to EclipseLink
as I found out that it support dynamic entity at the following links:
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic

Also, Graeme answered that it can be used with GORM at
http://grails.1312388.n4.nabble.com/Grails-and-EclipseLink-td2279539.html

Other references maybe useful (and I) to you to learn about
EclipseLink:
http://www.antoniogoncalves.org/xwiki/bin/view/Article/TestingJPA
http://www.dynamicjava.org/projects/dynamic-jpa

Hope this helps.

Regards,
Chee Kin

On Oct 20, 1:42 pm, Chee Kin Lim <limchee...@vobject.com> wrote:
> Hi there,
>
> Thanks for writing. Please see my reply below.
>
> Regards,
> Chee Kin
>
> On Tue, Oct 19, 2010 at 11:02 PM, caden whitaker
> <caden.whita...@gmail.com>wrote:
> > ((GroovyObject)gcl.parseClass(contents.toString()).newInstance()).invokeMet hod("test",
> > args);
>
> > Do you think this would be easy to implement without running grails,
> > just GORM?
>
> As you are using JPA, I think you not only need to dynamically compile the
> class you also need to register it to the JPA provider such as Hibernate. I
> think the post athttp://www.jroller.com/melix/entry/dynamic_database_models_with_groovyhave
> the solution you are looking for.
>
> I’ve been looking for an answer to this question for the
>
>
>
> > last week and I’m just going nowhere. Refrenced links:
>
> >http://groovy.329449.n5.nabble.com/Hibernate-Dynamic-Groovy-Class-Is-...
>
> >http://grails.1312388.n4.nabble.com/GORM-Dynamic-Groovy-Class-Is-this...
>
> >http://www.coderanch.com/t/513878/ORM/java/Hibernate-Dynamic-Groovy-C...

Chee Kin Lim

unread,
Oct 31, 2010, 3:18:35 AM10/31/10
to Grails Dynamic Domain Class Plugin
Not good. Just find out that EclipseLink doesn't support schema update as Hibernate do at http://stackoverflow.com/questions/2945714/eclipselink-update-existing-tables

This feature is required by Form Builder plugin I developed at http://code.google.com/p/grails-form-builder-plugin/

Chee Kin
Reply all
Reply to author
Forward
0 new messages