J
J
Jake Taylor2020-08-19 20:30:16
Java
Jake Taylor, 2020-08-19 20:30:16

Why can't an entity class exist inside another entity class?

There are 2 entity classes:
1) Point (point)

source

package entity;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Point {
    @Id
    private int id;

    private int x;
    private int y;

    public Point() {
    }

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}


2) Quadrangle (quadrilateral)
source

package entity;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * Entity-класс "Четырехугольник"
 */

@Entity
public class Quadrangle {
    @Id
    private int id;

    public Quadrangle() {
    }

    public Quadrangle(Point p1, Point p2, Point p3, Point p4) {

    }

    @Override
    public String toString() {
        return "Quadrangle id #" + id;
    }
}



The quadrilateral must have four points, i.e. accepts four Point entity classes in the constructor. But why can't you create variables of type Point inside the Quadrangle class? How then to use coordinates?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BorLaze, 2020-08-19
@BorLaze

Why not?
It is quite possible - a One-To-One connection is created, and forward. What is the problem?

R
Ruslan., 2020-08-19
@LaRN

These annotations are ultimately needed to describe the relationship between database entities.
One entity can be nested within another, but the relationship between them must be specified.
There is a description here:
https://www.tutorialspoint.com/jpa/jpa_entity_rela...
See annotations:
@ManyToOne Relation
@OneToMany Relation
@OneToOne Relation
@ManyToMany Relation

O
Orkhan, 2020-08-19
Hasanly @azerphoenix

My answer is not exactly the answer to your question...
but if you want to describe the essence of some geographic object and store it in the database, then look towards WKT || JTS.
For example, MULTIPOLYGON (30 31, 29 28, 27 26, 24 23)instead of Quadrangle
Well, POINT (30 31)instead of Point

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question