简体   繁体   中英

Designing JPA Entity for Database Table

I am designing a voting application and designed a database with 2 tables - State and District. The two tables are -

State details table

State_ID NUMBER(3)

State_name VARCHAR2(30)

PRIMARY KEY(State_ID)

District details table

District_ID NUMBER(3)

State_ID NUMBER(3)

District_name VARCHAR2(30)

PIN_Code NUMBER(6)

PRIMARY KEY(District_ID)

UNIQUE(State_ID, District_name)

FOREIGN KEY(State_ID)

As two states can have a district with same name, I am considering the combination of State ID and District name as UNIQUE by combination.

Now I have to design JPA Entities for these two tables, but I am unable to design because, in the database design, the District table has State ID in it as Foreign Key, but when it comes to designing entities having a State object inside District sounds meaningless, because if I keep HAS-A relationship in my mind, then District doesn't have a State in it. A State HAS-A list of Districts . This is not in accord with the above database design.

Can someone please help me in designing JPA Entities for this. Please suggest if the database design needs modification too. Thanks.

CREATE TABLE States (
    code CHAR(2) NOT NULL CHARACTER SET ascii,
    name VARCHAR(30) NOT NULL,
    PRIMARY KEY(code)
);
CREATE TABLE Districts
    id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    state_code CHAR(2) NOT NULL CHARACTER SET ascii,
    ...
    PRIMARY KEY(id),
    UNIQUE (state_code, name)
);

Notes:
* May as well use the standard state codes (AK, AL, ...) if you are talking about the US.
* Probably the only purpose of States is to spell out the name.
* I was explicit about ascii in case your table defaults to utf8.
* MySQL uses VARCHAR, not VARHAR2.
* You could get rid of Districts.id and have simply PRIMARY KEY(state_code, name) , but I am guessing you have a bunch of other tables that need to join to this one, and a 2-byte id would be better than the bulky alternative.

An example JPA approach based on Rick's proposal :

@Entity
@Table(name = "States")
public class State {
    @Id
    @Column(nullable = false, columnDefinition = "CHAR(2) CHARACTER SET ascii")
    private String code;

    @Column(nullable = false, length = 30)
    private String name;

    @OneToMany(mappedBy = "state")
    private Collection<District> districts;

    public State() { }

    // getters, setters, etc.
}
@Entity
@Table(name = "Districts", uniqueConstraints = { 
    @UniqueConstraint(columnNames = { "state_code", "name" })
})
public class District {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, columnDefinition = "SMALLINT UNSIGNED")
    private short id;

    @Column(name = "state_code", nullable = false, 
            columnDefinition = "CHAR(2) CHARACTER SET ascii")
    private String code;

    @Column(length = 30)
    private String name;

    @ManyToOne
    @JoinColumn(name = "state_id")
    private State state;

    public District() { }

    // getters, setters, etc.
}

NOTE: due to usage of columnDefinition attribute the above entities are not portable across databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM