You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Aron L <a....@gmail.com> on 2013/12/06 22:35:44 UTC

Using ReverseMappingTool.run(...) to generate Java strings?

Hi All,

I've managed to get ReverseMappingTool running from within a plain-old Java
method. Using the following: 

--------------------------------------------------------

JDBCConfiguration result = new JDBCConfigurationImpl();
result.setDBDictionary(xyz);
result.setConnectionURL(xyz);
result.setConnectionDriverName(xyz);
result.setConnectionUserName(xyz);
result.setConnectionPassword(xyz);

Options rmOpts = new Options();
String argString = "-Log DefaultLevel=INFO -metaDataFactory jpa() -metadata
none";
rmOpts.setFromCmdLine(StringUtils.split(argString, " "));

try { 
    ReverseMappingTool.run(result, new String[0], rmOpts);
} catch (Exception e) {
    e.printStackTrace();
}

--------------------------------------------------------

This successfully writes Java files reflective of the specified database to
the directory that my code is running in, as expected.

However, I would much prefer to have the Java generated returned in a map as
a String, than written to a file. I see a way of changing the code slightly
to make this happen: The method "recordCode" called by run on [1] below can
take a Map, and if the Map is there, it will write the code that I want to
that map. So if a variant of run(...) is added that allows the user to pass
a map which is then forwarded to that method, then the user will get the
code back.

[1]
http://grepcode.com/file/repository.springsource.com/org.apache.openjpa/com.springsource.org.apache.openjpa/1.1.0/org/apache/openjpa/jdbc/meta/ReverseMappingTool.java#2017

Does anyone see a better way to do this, that doesn't involve changing the
code? And if not, should I submit this customization as a patch?

Thanks,
Aron



--
View this message in context: http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Aron L <a....@gmail.com>.
Rick, 

That looks great. I like the move from:

+        if(output == null){
+            tool.recordCode();
+        } else{
+            tool.recordCode(output);
+        }

to 

+        tool.recordCode(output);

Many thanks for all the help. 

I thought you might enjoy this workaround I've been using in the meantime in
order to meet a deadline:

http://pastebin.com/X94iAnjB

Thanks,
Aron



--
View this message in context: http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730p7585764.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Rick Curtis <cu...@gmail.com>.
Take a look at https://issues.apache.org/jira/browse/OPENJPA-2466 to see if
that'll work for you.

Thanks,
Rick


On Tue, Dec 10, 2013 at 9:37 AM, Aron L <a....@gmail.com> wrote:

> Rick,
>
> That does what I need for now, but might I also suggest adding another
> method that parallels the one you just suggested: run(JDBCConfiguration
> conf, String[] args, Options opts, Map output)? Or combining the two, say:
> run(JDBCConfiguration conf, String[] args, Flags flags, Options opts, Map
> output)? It seems like there is a good bit of code in
> run(JDBCConfiguration,
> String[], Options) that one would want to reuse in line with the Map.
>
> -Aron
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730p7585752.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
*Rick Curtis*

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Aron L <a....@gmail.com>.
Rick,

That does what I need for now, but might I also suggest adding another
method that parallels the one you just suggested: run(JDBCConfiguration
conf, String[] args, Options opts, Map output)? Or combining the two, say:
run(JDBCConfiguration conf, String[] args, Flags flags, Options opts, Map
output)? It seems like there is a good bit of code in run(JDBCConfiguration,
String[], Options) that one would want to reuse in line with the Map.

-Aron



--
View this message in context: http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730p7585752.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Rick Curtis <cu...@gmail.com>.
I would rather add an additional static run method[1] that has an
additional parameter to the existing parameter list and that can be used
for output.

http://pastebin.com/jZ7vUtM8

Thanks,
Rick



On Mon, Dec 9, 2013 at 4:29 PM, Aron L <a....@gmail.com> wrote:

> Rick,
>
> That could work but I would end up using JDBCConfiguration in an unusual
> way
> to get the result I want, something like this:
>
> http://pastebin.com/Uqngcuaj
>
> I'm not sure if this would be a lasting fix; there's nothing in the method
> contract that ensures the added value will be propagated through the other
> run methods.
>
> An easier (for me) way to do this might be adding a flag of type Map to
> ReverseMappingTool.Flags, and if that flag is not null, calling
> tool.recordCode(Map) instead of tool.recordCode() where I linked before.
> However, I could see where this might be an issue as the flag wouldn't
> really be usable from the command line.
>
> Thanks,
> Aron
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730p7585750.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
*Rick Curtis*

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Aron L <a....@gmail.com>.
Rick,

That could work but I would end up using JDBCConfiguration in an unusual way
to get the result I want, something like this:

http://pastebin.com/Uqngcuaj

I'm not sure if this would be a lasting fix; there's nothing in the method
contract that ensures the added value will be propagated through the other
run methods.

An easier (for me) way to do this might be adding a flag of type Map to
ReverseMappingTool.Flags, and if that flag is not null, calling
tool.recordCode(Map) instead of tool.recordCode() where I linked before.
However, I could see where this might be an issue as the flag wouldn't
really be usable from the command line. 

Thanks,
Aron



--
View this message in context: http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730p7585750.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Rick Curtis <cu...@gmail.com>.
Would something like this work for you?

http://pastebin.com/gwrFg48c

Thanks,
Rick


On Mon, Dec 9, 2013 at 10:19 AM, Aron L <a....@gmail.com> wrote:

> Rick,
>
> That would be much appreciated - thank you. Please let me know if I can
> help
> at all.
>
> -Aron
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730p7585742.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
*Rick Curtis*

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Aron L <a....@gmail.com>.
Rick,

That would be much appreciated - thank you. Please let me know if I can help
at all.

-Aron



--
View this message in context: http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730p7585742.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Rick Curtis <cu...@gmail.com>.
Aron -

If you're using trunk I could modify ReverseMappingTool to allow for
extension. Let me know if you want me to put something in.

Thanks,
Rick


On Sun, Dec 8, 2013 at 3:50 PM, Aron Lurie <ar...@cambridgesemantics.com>wrote:

> Rick,
> Thanks for the idea. I did some reading into that, and while there is some
> potential there to extend ReverseMappingTool and override the recordCode()
> method, the static run(...) method itself is responsible for instantiating
> the class [1]. So, without copying all of that method into the subclass, no
> dice.
>
> [1]
>
> http://grepcode.com/file/repository.springsource.com/org.apache.openjpa/com.springsource.org.apache.openjpa/1.1.0/org/apache/openjpa/jdbc/meta/ReverseMappingTool.java#1987
>
> Thanks,
> Aron
>
>
> On Sat, Dec 7, 2013 at 12:54 PM, Rick Curtis <cu...@gmail.com> wrote:
>
> > Have you tried to extend the ReverseMappingTool so that you can intercept
> > the generated code rather than write it to a file? ... I'm not sure
> > how feasible this might be as I'm writing from my phone.
> >
> >
> > On Fri, Dec 6, 2013 at 3:35 PM, Aron L <a....@gmail.com> wrote:
> >
> > > Hi All,
> > >
> > > I've managed to get ReverseMappingTool running from within a plain-old
> > Java
> > > method. Using the following:
> > >
> > > --------------------------------------------------------
> > >
> > > JDBCConfiguration result = new JDBCConfigurationImpl();
> > > result.setDBDictionary(xyz);
> > > result.setConnectionURL(xyz);
> > > result.setConnectionDriverName(xyz);
> > > result.setConnectionUserName(xyz);
> > > result.setConnectionPassword(xyz);
> > >
> > > Options rmOpts = new Options();
> > > String argString = "-Log DefaultLevel=INFO -metaDataFactory jpa()
> > -metadata
> > > none";
> > > rmOpts.setFromCmdLine(StringUtils.split(argString, " "));
> > >
> > > try {
> > >     ReverseMappingTool.run(result, new String[0], rmOpts);
> > > } catch (Exception e) {
> > >     e.printStackTrace();
> > > }
> > >
> > > --------------------------------------------------------
> > >
> > > This successfully writes Java files reflective of the specified
> database
> > to
> > > the directory that my code is running in, as expected.
> > >
> > > However, I would much prefer to have the Java generated returned in a
> map
> > > as
> > > a String, than written to a file. I see a way of changing the code
> > slightly
> > > to make this happen: The method "recordCode" called by run on [1] below
> > can
> > > take a Map, and if the Map is there, it will write the code that I want
> > to
> > > that map. So if a variant of run(...) is added that allows the user to
> > pass
> > > a map which is then forwarded to that method, then the user will get
> the
> > > code back.
> > >
> > > [1]
> > >
> > >
> >
> http://grepcode.com/file/repository.springsource.com/org.apache.openjpa/com.springsource.org.apache.openjpa/1.1.0/org/apache/openjpa/jdbc/meta/ReverseMappingTool.java#2017
> > >
> > > Does anyone see a better way to do this, that doesn't involve changing
> > the
> > > code? And if not, should I submit this customization as a patch?
> > >
> > > Thanks,
> > > Aron
> > >
> > >
> > >
> > > --
> > > View this message in context:
> > >
> >
> http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730.html
> > > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> > >
> >
> >
> >
> > --
> > *Rick Curtis*
> >
>



-- 
*Rick Curtis*

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Aron Lurie <ar...@cambridgesemantics.com>.
Rick,
Thanks for the idea. I did some reading into that, and while there is some
potential there to extend ReverseMappingTool and override the recordCode()
method, the static run(...) method itself is responsible for instantiating
the class [1]. So, without copying all of that method into the subclass, no
dice.

[1]
http://grepcode.com/file/repository.springsource.com/org.apache.openjpa/com.springsource.org.apache.openjpa/1.1.0/org/apache/openjpa/jdbc/meta/ReverseMappingTool.java#1987

Thanks,
Aron


On Sat, Dec 7, 2013 at 12:54 PM, Rick Curtis <cu...@gmail.com> wrote:

> Have you tried to extend the ReverseMappingTool so that you can intercept
> the generated code rather than write it to a file? ... I'm not sure
> how feasible this might be as I'm writing from my phone.
>
>
> On Fri, Dec 6, 2013 at 3:35 PM, Aron L <a....@gmail.com> wrote:
>
> > Hi All,
> >
> > I've managed to get ReverseMappingTool running from within a plain-old
> Java
> > method. Using the following:
> >
> > --------------------------------------------------------
> >
> > JDBCConfiguration result = new JDBCConfigurationImpl();
> > result.setDBDictionary(xyz);
> > result.setConnectionURL(xyz);
> > result.setConnectionDriverName(xyz);
> > result.setConnectionUserName(xyz);
> > result.setConnectionPassword(xyz);
> >
> > Options rmOpts = new Options();
> > String argString = "-Log DefaultLevel=INFO -metaDataFactory jpa()
> -metadata
> > none";
> > rmOpts.setFromCmdLine(StringUtils.split(argString, " "));
> >
> > try {
> >     ReverseMappingTool.run(result, new String[0], rmOpts);
> > } catch (Exception e) {
> >     e.printStackTrace();
> > }
> >
> > --------------------------------------------------------
> >
> > This successfully writes Java files reflective of the specified database
> to
> > the directory that my code is running in, as expected.
> >
> > However, I would much prefer to have the Java generated returned in a map
> > as
> > a String, than written to a file. I see a way of changing the code
> slightly
> > to make this happen: The method "recordCode" called by run on [1] below
> can
> > take a Map, and if the Map is there, it will write the code that I want
> to
> > that map. So if a variant of run(...) is added that allows the user to
> pass
> > a map which is then forwarded to that method, then the user will get the
> > code back.
> >
> > [1]
> >
> >
> http://grepcode.com/file/repository.springsource.com/org.apache.openjpa/com.springsource.org.apache.openjpa/1.1.0/org/apache/openjpa/jdbc/meta/ReverseMappingTool.java#2017
> >
> > Does anyone see a better way to do this, that doesn't involve changing
> the
> > code? And if not, should I submit this customization as a patch?
> >
> > Thanks,
> > Aron
> >
> >
> >
> > --
> > View this message in context:
> >
> http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>
>
>
> --
> *Rick Curtis*
>

Re: Using ReverseMappingTool.run(...) to generate Java strings?

Posted by Rick Curtis <cu...@gmail.com>.
Have you tried to extend the ReverseMappingTool so that you can intercept
the generated code rather than write it to a file? ... I'm not sure
how feasible this might be as I'm writing from my phone.


On Fri, Dec 6, 2013 at 3:35 PM, Aron L <a....@gmail.com> wrote:

> Hi All,
>
> I've managed to get ReverseMappingTool running from within a plain-old Java
> method. Using the following:
>
> --------------------------------------------------------
>
> JDBCConfiguration result = new JDBCConfigurationImpl();
> result.setDBDictionary(xyz);
> result.setConnectionURL(xyz);
> result.setConnectionDriverName(xyz);
> result.setConnectionUserName(xyz);
> result.setConnectionPassword(xyz);
>
> Options rmOpts = new Options();
> String argString = "-Log DefaultLevel=INFO -metaDataFactory jpa() -metadata
> none";
> rmOpts.setFromCmdLine(StringUtils.split(argString, " "));
>
> try {
>     ReverseMappingTool.run(result, new String[0], rmOpts);
> } catch (Exception e) {
>     e.printStackTrace();
> }
>
> --------------------------------------------------------
>
> This successfully writes Java files reflective of the specified database to
> the directory that my code is running in, as expected.
>
> However, I would much prefer to have the Java generated returned in a map
> as
> a String, than written to a file. I see a way of changing the code slightly
> to make this happen: The method "recordCode" called by run on [1] below can
> take a Map, and if the Map is there, it will write the code that I want to
> that map. So if a variant of run(...) is added that allows the user to pass
> a map which is then forwarded to that method, then the user will get the
> code back.
>
> [1]
>
> http://grepcode.com/file/repository.springsource.com/org.apache.openjpa/com.springsource.org.apache.openjpa/1.1.0/org/apache/openjpa/jdbc/meta/ReverseMappingTool.java#2017
>
> Does anyone see a better way to do this, that doesn't involve changing the
> code? And if not, should I submit this customization as a patch?
>
> Thanks,
> Aron
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Using-ReverseMappingTool-run-to-generate-Java-strings-tp7585730.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
*Rick Curtis*