database design - How do I express a polymorphic association in JPA? -


A is similar to a foreign key or a lot of relationships, the difference is that one of the targets could be Many types (classes in language, tables in DB).

I have been using PHP from Java for a few years, in the old code, I did my own ORM roll, which was not optimal for several reasons. Although I can begin to change things later, and maybe things have started implementing itself, because now I would like to use off-shelf ORM and JPA on my unit classes.

Now, one thing about database layout which I do not know how to express in JPA:

I have a node and one < Code> edge table that stores a graph (a drag, if it counts). Each node can optionally refer to another entity from the database. These relationships can be refreshed several times during the entire graph and there may also be "orphans", which will not be suitable for the user, but which will keep the minimum for some time.

These objects are not all related in the context of Warsaw etc. But there is a natural hierarchy similar to the customer-> site-> floor-> room. In fact, years ago, I had just started with foreign key areas pointing to "major" objects. However, this hierarchy is not flexible enough and starts to fall apart. For example, I want to allow users to group objects in folders, some objects can have many "parents" and can change relationships over time. I have to keep an eye on how to use the relationship, so the graphs of the graphs are time-bound time associated with them, which states that when it was valid for that shore.

An object linked from one node is stored in two columns of node table, one gets the ID in the foreign table, one is named after it. For example (some columns skipped):

  Table node: + -------- + ------- + --------- - + | IxNode | IxRef | SRefType | + -------- + ------- + ---------- + | 1 | Faucet Faucet & Lt; - It looks like a "folder". 2 | 17 | Source | | 3 | 58 Series | & Lt; - There are seven types of related objects so far - + -------- + ------- + ---------- + Table Source (excerpt): + - --- ----- + -------------------- + | IxSource | Sameem | + ---------- + -------------------- + | 16 | 4th floor breaker | 17 | 5th floor breaker | 18 | 6th floor breaker. + ---------- + -------------------- +  

There may be a different solution Using JPAs, I can change something about table layouts or present a new table etc. However, I have already thought a lot about this and table structure seems to me that there might be a third way that I did not think.

I think you have already hit an answer on an abstract class (either @ Antitee or @ MappsUpClass) and expand the different types.

Something can work like this

  @MappedSuperclass @ Inheritance (strategy = inheritance type TABLE_PER_CLASS) public abstract class edge {//. . . @OneToMany archive & lt; Node & gt; Nodes; } @Entity Public Class Source Expansion Edge {} @Entity Public Class Series Edge Spans {} @Entity Public Class Node {//. . . @ManyToOne edge edge; }  

I think that you may not want to have any connection between the source and the series, but the only way to expand a common abstract (table-less) class is to Can you think of thinking you want?

Inheritance type. TABLE_PER_CLASS will place the source and series in separate tables (you can use SINGLE_TABLE to do something like the previous answer).

If this is not for you, many JPA providers provide a tool that creates mapping based on the current set of tables. In OpenJapa it is called reverse mapping tools [1], the tool will generate Java source files which you can use as the starting point for your mapping. I have some doubts about Hibernate or Eclase Link, but you can only use OpenJPA and use unit definitions with a different provider (the device does not know any openJPA specific code yet).

[1]


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -