Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol Buffers with JPA

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?

like image 693
Yrlec Avatar asked Jul 18 '09 20:07

Yrlec


People also ask

Can I use Protobuf with REST API?

Yes, you can absolutely combine Protobuf and REST.

Is Protobuf more efficient than JSON?

Protobuf, the binary format crafted by Google, surpasses JSON performance even on JavaScript environments like Node. js/V8 and web browsers.

What is Protocol buffers Java?

Protocol buffers are a combination of the definition language (created in . proto files), the code that the proto compiler generates to interface with data, language-specific runtime libraries, and the serialization format for data that is written to a file (or sent across a network connection).

What are Protocol buffers used for?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.


1 Answers

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.

like image 53
Dave Avatar answered Sep 17 '22 14:09

Dave