You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by "Gilberto C. Andrade" <gi...@gmail.com> on 2009/08/07 20:57:18 UTC

Cayenne POJO inheritance

Hi, sometime ago I've tried cayenne JPA [1] and that time I couldn't
touch POJO source code.
Now this is a personal learning and I want do the cayenne way.

So trying Andrus advices:

1 reverse engineer the DB in question with CayenneModeler
2 manually tweak the model to match the property names in the existing
POJO Java classes
3 Run class generator to generate the _Xyz superclasses.
4 Change the existing classes by hand to inherit from _Xyz counterpart.
5 Remove existing persistence fields, and use superclass getters and
setters to access them. * Change the API throughout the code to use
Cayenne ObjectContext and Queries.

I've stopped on 4 when I saw the source generated
(osu-park/src/main/resources/codigo). The class inheritance looks a
little odd.

While testing my original code I do this
(osu-park/src/test/java/park/model/party/PessoaTest.java):
[code]
public class PessoaTest {

	@Test
	public final void testPessoaFisica() {
		Pessoa p;
		p = new PessoaFisica(1, "Gilberto", "F", "Endereço",
Date.valueOf("2009-01-01"), "RG", "CPF");
		assertNotNull(p);
	}

	@Test
	public final void testPessoaJuridica() {
		Pessoa p;
		p = new PessoaJuridica(1, "Souza e cia", "J", "Endereço",
Date.valueOf("2009-01-01"), "CNPJ");
		assertNotNull(p);
	}
}
[/code]

How would it be with the new generated code? I think I need a good reference!

Thanks,

Gilberto C. Andrade
blog.gilbertoca.com


[1]http://markmail.org/message/ijogqds67xhkri2u

Re: Cayenne POJO inheritance

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 8/08/09 6:51 AM, Tore Halset wrote:
>
> Take a look at the examples: (the formatting of the code-examples look
> strange..)

Hmmm... yes. This would be the Confluence upgrade. I'll see if I can fix that this weekend.

Ari



-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Cayenne POJO inheritance

Posted by "Gilberto C. Andrade" <gi...@gmail.com>.
On Sun, Aug 16, 2009 at 3:21 AM, Aristedes Maniatis<ar...@maniatis.org> wrote:
> On 15/08/09 5:41 AM, Gilberto C. Andrade wrote:
>>
>> Ok, after a good read on [1] and [2], I could understand that what I
>> want to do [3], right now, is not implemented in cayenne.  Cayenne
>> only supply table-per-class-hierarchy feature.
>> Is that right?
>>
>> Gilberto
>>
>> [1]http://cayenne.apache.org/doc/inheritance-overview.html
>> [2]http://cayenne.apache.org/doc/modeling-inheritance.html
>> [3]http://edocs.bea.com/kodo/docs324/img/inheritance-vertical.png
>
> Horizontal inheritance is also implemented now (3.0M6). However, you should
> watch the jira task [4] to see when vertical inheritance is being worked on.

Thanks, I will!

> In the short term, you may be able to implement what you need using the
> other two ways to model inheritance; for the most part, your Java code will
> not change when you change your database implementation of how the
> inheritance is represented.

Most of the time you can't change the database, but thanks God it
isn't my problem right now.
I'm doing a simple guide on how to develop - in Brazilian Portuguese
language - with apache cayenne and click projects, I will use this one
[1], developed with Delphi written by Adail Muniz Retamal, as
reference.
I want to say that I found  some difficulty with cayenne
documentation, most of the time I wasted looking for the information I
wanted. This article is a valid cayenne reference [2]? It seems
outdated! I've found it very simple and comprehensive.  It could be a
fault from myself , not knowing or understanding the general cayenne
documentation idea. Or I was contaminated by the simplicity of click
documentation [3].

Thankful for your attention,

Gilberto


[1] http://edn.embarcadero.com/article/33740
[2] http://www.theserverside.com/tt/articles/article.tss?l=Cayenne
[3] http://incubator.apache.org/click/docs/user-guide.html|http://incubator.apache.org/click/docs/quick-start.html

>
>
> Ari Maniatis
>
>
>
> [4] https://issues.apache.org/jira/browse/CAY-1090
>
> -------------------------->
> Aristedes Maniatis
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>

Re: Cayenne POJO inheritance

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 15/08/09 5:41 AM, Gilberto C. Andrade wrote:
> Ok, after a good read on [1] and [2], I could understand that what I
> want to do [3], right now, is not implemented in cayenne.  Cayenne
> only supply table-per-class-hierarchy feature.
> Is that right?
>
> Gilberto
>
> [1]http://cayenne.apache.org/doc/inheritance-overview.html
> [2]http://cayenne.apache.org/doc/modeling-inheritance.html
> [3]http://edocs.bea.com/kodo/docs324/img/inheritance-vertical.png

Horizontal inheritance is also implemented now (3.0M6). However, you should watch the jira task [4] to see when vertical inheritance is being worked on. In the short term, you may be able to implement what you need using the other two ways to model inheritance; for the most part, your Java code will not change when you change your database implementation of how the inheritance is represented.


Ari Maniatis



[4] https://issues.apache.org/jira/browse/CAY-1090

-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Cayenne POJO inheritance

Posted by "Gilberto C. Andrade" <gi...@gmail.com>.
Ok, after a good read on [1] and [2], I could understand that what I
want to do [3], right now, is not implemented in cayenne.  Cayenne
only supply table-per-class-hierarchy feature.
Is that right?

Gilberto

[1] http://cayenne.apache.org/doc/inheritance-overview.html
[2] http://cayenne.apache.org/doc/modeling-inheritance.html
[3] http://edocs.bea.com/kodo/docs324/img/inheritance-vertical.png


On Mon, Aug 10, 2009 at 2:49 PM, Gilberto C.
Andrade<gi...@gmail.com> wrote:
> Thanks Tore, but my initial problem is understand Andrus's tips:
> 3 Run class generator to generate the _Xyz superclasses.
> 4 Change the existing classes by hand to inherit from _Xyz counterpart.
> 5 Remove existing persistence fields, and use superclass getters and
> setters to access them. * Change the API throughout the code to use
> Cayenne ObjectContext and Queries.
>
> In my model I have this hierarchy (implemented):
>
> Pessoa->PessoaFisica
> Pessoa->PessoaJuridica
>
> Cayenne Modeler generated:
>
> public abstract class Pessoa extends _Pessoa
> public class PessoaFisica extends _PessoaFisica
> public class PessoaJuridica extends _PessoaJuridica
>
> and this one into auto package:
>
> public abstract class _Pessoa extends CayenneDataObject
> public abstract class _PessoaFisica extends Pessoa    <========== HERE
> IS THE POINT
> public abstract class _PessoaJuridica extends Pessoa <========== HERE
> IS THE POINT
>
> Is that all ok? Shouldn't [_PessoaFisica extends Pessoa] be
> [_PessoaFisica extends _Pessoa ]?
>
> Gilberto
>

Re: Cayenne POJO inheritance

Posted by "Gilberto C. Andrade" <gi...@gmail.com>.
Thanks Tore, but my initial problem is understand Andrus's tips:
3 Run class generator to generate the _Xyz superclasses.
4 Change the existing classes by hand to inherit from _Xyz counterpart.
5 Remove existing persistence fields, and use superclass getters and
setters to access them. * Change the API throughout the code to use
Cayenne ObjectContext and Queries.

In my model I have this hierarchy (implemented):

Pessoa->PessoaFisica
Pessoa->PessoaJuridica

Cayenne Modeler generated:

public abstract class Pessoa extends _Pessoa
public class PessoaFisica extends _PessoaFisica
public class PessoaJuridica extends _PessoaJuridica

and this one into auto package:

public abstract class _Pessoa extends CayenneDataObject
public abstract class _PessoaFisica extends Pessoa    <========== HERE
IS THE POINT
public abstract class _PessoaJuridica extends Pessoa <========== HERE
IS THE POINT

Is that all ok? Shouldn't [_PessoaFisica extends Pessoa] be
[_PessoaFisica extends _Pessoa ]?

Gilberto

Re: Cayenne POJO inheritance

Posted by Tore Halset <ha...@pvv.ntnu.no>.
Hello.

The cayenne way is like this:

ObjectContext ctxt = ....
Pessoa p = (Pessoa) ctxt.newObject(PessoaFisica.class);
p.setName("Gilberto");
p.setDateOfBirth(Date.valueOf("2009-01-01"));
ctxt.commitChanges();

It seem strange at the beginning, but there are lots of benefits with  
the context keeping track of the objects.

Take a look at the examples: (the formatting of the code-examples look  
strange..)
http://cayenne.apache.org/doc/tutorial-dataobjects.html

  - Tore.

On Aug 7, 2009, at 20:57, Gilberto C. Andrade wrote:

> 		Pessoa p;
> 		p = new PessoaFisica(1, "Gilberto", "F", "Endereço",
> Date.valueOf("2009-01-01"), "RG", "CPF");
> 		assertNotNull(p);