"Architectural" question - Java -> proto -> C#

93 views
Skip to first unread message

roberto_sc

unread,
Sep 13, 2010, 3:09:00 PM9/13/10
to Protocol Buffers
Hi

I have a basic question about how to organize my project.
I have a client running C# code, a server running Java code and I
intend to use protocol buffers to exchange data.
I thought I could use the .proto file to describe the classes of my
datamodel and generate .java and .cs and then use these generated src
as my datamodel. But protoc generate code for message exchange and
cannot be edited, for example, I cannot generate java code for my
Person class and add the annotations to persist using JPA.

So, the question is, do I have to mantain 3 files - .java, .cs
and .proto - that represent the same thing?

Thanks

Kenton Varda

unread,
Sep 14, 2010, 2:19:49 PM9/14/10
to roberto_sc, Protocol Buffers
I'm not all that familiar with JPA, but my guess is that applying JPA to a protocol buffer type is going to be much less efficient than using protobuf's native encoding.  So you probably want to be serializing your protobufs with .toByteArray() and then persisting that.

Failing that, you might want to look at the protobuf reflection API (not to be confused with java reflection) to see if it might be part of a solution:

E.g. maybe you could call message.getAllFields() and then feed the data into JPA somehow.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Roberto Caldas

unread,
Sep 14, 2010, 3:27:22 PM9/14/10
to Kenton Varda, Protocol Buffers
Yes, I tried to use the reflection API, but this doesn't seem right to me because I have to mantain 3 classes for the same entity. When I add a new attribute I'll have to update 3 files! I know I really cannot use JPA with the java code generated by protoc, but using the reflection API the best way?  I was used to a java-only world where I could automatically transform my objects into xml messages having only one class representing one entity for all project.

About my try on the reflection API, I did not succed and I posted one question about it, could you please help me? I understood that I had to use the protobuf reflection together with java reflection:
http://groups.google.com/group/protobuf/browse_thread/thread/add9295e2151481e

Thank you for your reply.
--
virtus in medium est
.

Kenton Varda

unread,
Sep 14, 2010, 7:56:37 PM9/14/10
to Roberto Caldas, Protocol Buffers
On Tue, Sep 14, 2010 at 12:27 PM, Roberto Caldas <roberto...@gmail.com> wrote:
Yes, I tried to use the reflection API, but this doesn't seem right to me because I have to mantain 3 classes for the same entity. When I add a new attribute I'll have to update 3 files!

If you're using the reflection API correctly, that shouldn't be necessary.
 
I know I really cannot use JPA with the java code generated by protoc, but using the reflection API the best way?  I was used to a java-only world where I could automatically transform my objects into xml messages having only one class representing one entity for all project.

About my try on the reflection API, I did not succed and I posted one question about it, could you please help me? I understood that I had to use the protobuf reflection together with java reflection:
http://groups.google.com/group/protobuf/browse_thread/thread/add9295e2151481e

You shouldn't be using java reflection, only protobuf reflection.

Your earlier post attempted to treat the protobuf as a JavaBean.  That won't work; protobufs are not beans.  You need to write code that actually calls message.listAllFields() and iterates over the returned map to handle each field.

David Yu

unread,
Sep 15, 2010, 2:10:49 AM9/15/10
to Roberto Caldas, Kenton Varda, Protocol Buffers
For existing java beans, http://code.google.com/p/protostuff might be able to help.
When the cat is away, the mouse is alone.
- David Yu

Roberto Caldas

unread,
Sep 15, 2010, 11:41:24 AM9/15/10
to David Yu, Kenton Varda, Protocol Buffers
> If you're using the reflection API correctly, that shouldn't be necessary.
There is something very wrong here, because I just can't see this :(  I must have a simple POJO Person.java with JPA annotations in it, I cannot persist a byte array of my message, otherwise I wouldn't be able to make SQL queries on it. So, this class Person.java is not the class generated by protoc, I have another class PersonMessage.java generated by protoc and I want to copy all the values from an PersonMessage instance to a Person instance. Both classes have attributes with the same name.
I didn't succed to do this with reflections, I would have to write a considerably complex code to cover all the cases (all types of fields inside the protobuf: messages inside messages, array of messages inside the message, etc), as I see, it's not just a question of iterating over a Map of fields.


 >For existing java beans, http://code.google.com/p/protostuff might be able to help.
This is nice, it resolves the problem for the java side, but I still have the same problem on my client that uses C# :(

Thanks

Kenton Varda

unread,
Sep 21, 2010, 3:41:44 PM9/21/10
to Roberto Caldas, David Yu, Protocol Buffers
It's true that you wouldn't be able to take advantage of the JPA annotations.  However, I'd imagine JPA provides a lower-level API that lets you use it with objects that aren't annotated.

wangscu

unread,
Sep 27, 2010, 5:36:12 AM9/27/10
to Protocol Buffers
hi, we aready done that.
at taobao.com every rpc services was written by pojo, and if c, php
want invoke it ,it use web service.
but now we use pb .
the method is
generate a proto service by a java interface
and then you can generate other code by the proto.

Brandon Park

unread,
Jan 3, 2020, 10:16:02 PM1/3/20
to Protocol Buffers
I'm trying to solve this exact same problem and was wondering how to use compiled proto code with JPA and if there have been any developments on this since 10 years ago.  Thanks

Marc Gravell

unread,
Jan 4, 2020, 4:28:05 AM1/4/20
to Brandon Park, Protocol Buffers
The context here seems to be: "I have .proto file/files, and (for some reason) I need to make additional tweaks to them in java/c#", right?

In the case of C#, "partial classes" may be your friend, especially if all you are trying to do is add annotations/attributes. The tooling may also generate "partial methods", but I haven't checked recently. No idea if something similar exists in java.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/addf93b3-53d2-4b6b-aaeb-933aa0e7a9a4%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages