What is the appropriate way to store extended classes?

Consider the diagram:

image

I work with JPA for a short time, and until now I have never had a need to continue extended classes ... As you can see in the example SNMPNode, IPNodeetc. all extended classes from Node, which are also extended from GeoLocation.

I understand that I can annotate the main classes with @MappedSuperclassand IPNodeand SNMPNode, inheriting their properties for saving ... But in this case I get almost identical tables and, to my understanding, instead I could just group all the information in Node and work with one table.

Is this because the persistence of extended classes in JPA's work, or are my concepts wrong?

Same as the renewed part of the code:

public class Node extends GeoLocation {
    private String name;
    private Group group;
    private Location location;
    private Type type;
    private Company company;
}

public class IPNode extends Node {
    private Long id;
    private String ipAddress;
}

public class SNMPNode extends Node {
    private Long id;
    private SNMPServer server;
}

[[ ]]

, , :

INode:

public interface INode {
    public Long getId();
    public void setId(Long id);

    public String getName();
    public void setName(String name);

    public String getIpAddress();
    public void setIpAddress(String ipAddress);

    public String getCommunity();
    public void setCommunity(String community);
}

Node:

@Entity
@DiscriminatorValue("N")
@DiscriminatorColumn(name="NODE_TYPE",discriminatorType=DiscriminatorType.STRING, length=20)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class Node extends GeoLocation implements INode {
    @Id
    @GeneratedValue
    private Long id;
    private String name;

    public Long getId() {return id;}
    public void setId(Long id) {this.id = id;}

    public String getName() {return name;}
    public void setName(String name) {this.name = name;}

    (... Overrides from INode ...)
}

IPNode:

@Entity
@DiscriminatorValue("I")
public class IPNode extends Node {
    private String ipAddress;

    public String getIpAddress() { return this.ipAddress;}
    public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; }

    (... Overrides from INode ...)
}

SNMPNode:

@Entity
@DiscriminatorValue("S")
public class SNMPNode extends Node {
    private String community;

    public String getCommunity() { return community;}
    public void setCommunity(String community) { this.community = community; }

    (... Overrides from INode ...)
}

NodeRepository:

@Repository
public interface NodeRepository extends JpaRepository<Node, Long> { }

, :

@ContextConfiguration("classpath:/spring/application-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class NodeRepositoryTest {

    @Autowired
    NodeRepository repo;

    private INode node;

    @Before
    @Transactional
    @Rollback(false)
    public void setup() {
        node = new IPNode();
        node.setName("ipNode");
        node.setIpAddress("1.1.1.1");
        repo.save((IPNode)node);

        node = new SNMPNode();
        node.setName("snmpNode");
        node.setIpAddress("2.2.2.2");
        node.setCommunity("some text");
        repo.save((SNMPNode)node);
    }

    @Test
    @Transactional
    public void Test() throws Exception {
        INode testNode = repo.findOne(1L);
        assertNotNull(testNode);
    }
}

Node , ... URL- REST /nodes/ 1 /nodes/2, ...

:)

+4
1

@MappedSuperclass, . @MappedSuperclass , .

( ) , -nullable ( ).

, , FK, , . @MappedSuperclass, , , .

+5

All Articles