views:

189

answers:

1

I'm currently building a P2P-system which uses Protocol Buffers for all communication between the peers. There's also a some centralized parts of the system where the peers communicate with a server. The server uses JPA to store the data it has about the peers.

So essentially the clients has its data in Protocol Buffers-messages and the server in JPA-entities. My question is therefore if there's a way to automatically convert between them?

What I'm looking for is:

  • A generator of JPA-entities from .protos-files OR
  • A generator of .protos-files from JPA-entities AND
  • Some way to automatically convert between them at runtime so I don't have create the code to do it manually for every new class.

Ideally you could take it one step further and have them both be the same classes (i.e. Protocol Buffers with JPA annotations) but if that's not attainable the above would work as well.

Has this been done and if not do you think it's feasible to implement in a reasonable amount of time?

+1  A: 

The Java Protocol Buffers API wants to generate the classes. So it seems like it would be hard to then annotate those. Instead, you could start with your own XML "schema", and then have XSLT stylesheets that:

  1. Creates a .proto file
  2. Creates a JPA entity mapping XML file.

You could then use the google generated files with JPA.

Dave