JPA实体模型

问题描述 投票:0回答:0
@Entity @Table(name = "USER") public class User implements Serializable { private static final long serialVersionUID = -4827645864353922449L; @Id @GeneratedValue(generator = "USER_SEQ", strategy = GenerationType.SEQUENCE) @SequenceGenerator(name = "USER_SEQ", sequenceName = "USER_SEQ", allocationSize = 1, initialValue = 1) @Column(name = "USER_ID") private long userId; @Column(name = "NAME", length = 50) private String name; @Column(name = "SURNAME", length = 50) private String surname; @Column(name = "FISCAL_CODE", length = 50) private String cf; public Utente(String nome, String cognome, String cf) { setNome(nome); setCognome(cognome); setCf(cf); } public Utente() { super(); } public long getUtenteId() { return utenteId; } public void setUtenteId(long utenteId) { this.utenteId = utenteId; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public String getCognome() { return cognome; } public void setCognome(String cognome) { this.cognome = cognome; } public String getCf() { return cf; } public void setCf(String cf) { this.cf = cf; } @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; User u = (User) o; return userId == u.userId && Objects.equals(name, u.name) && Objects.equals(surname, u.surname) && Objects.equals(cf, u.cf); } @Override public int hashCode() { return Objects.hash(userId, name, surname, cf); } }

office

@Entity @Table(name = "OFFICE") public class Office implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "CODE", length = 100) private String code; @Column(name = "DESCRIPTION", length = 100) private String description; @ManyToMany(mappedBy = "offices") private List<Local> locals; public Office() { super(); } public Office(String code, String description, List<Local> locals) { super(); this.code = code; this.description = description; this.locals = locals; } public Office(String code) { this.code = code; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public List<Local> getLocals() { return locals; } public void setLocals(List<Local> locals) { this.locals = locals; } @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Office off = (Office) o; return Objects.equals(code, off.code) && Objects.equals(description, off.description) && Objects.equals(locals, off.locals); } @Override public int hashCode() { return Objects.hash(code, description, locals); } }
local

@Entity @Table(name = "LOCAL") public class Local implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "CODE", length = 50, nullable = false) private String code; @Column(name = "DESCRIPTION", length = 100, nullable = false) private String description; @ManyToMany @JoinTable(name = "ASS_OFF_LOC", joinColumns = @JoinColumn(name = "CODE"), inverseJoinColumns = @JoinColumn(name = "CODE_OFFICE")) private List<Ufficio> offices; public Local(String code) { this.code = code; } public Local() { super(); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public List<Office> getOffices() { return offices; } public void setOffices(List<Office> offices) { this.offices = offices; } @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Local immobile = (Local) o; return Objects.equals(code, immobile.code) && Objects.equals(description, immobile.description) && Objects.equals(offices, immobile.offices); } @Override public int hashCode() { return Objects.hash(code, description, offices); } }

ASS_OFF_LOC

@Entity @Table(name = "ASS_OFF_LOC") @IdClass(AssOrgLocId.class) public class AssOffLoc implements Serializable { private static final long serialVersionUID = -2217545215089280956L; @Id @ManyToOne @JoinColumn(name = "CODE_OFFICE") private Office office; @Id @ManyToOne @JoinColumn(name = "CODE_LOCAL") private Local local; public AssOffLoc(String codiceOffice, String codiceSigma) { this.office = new Office(codiceOffice); this.local = new Local(codiceSigma); } public AssOffLoc(Office office, Local local) { super(); this.office = office; this.local = local; } public AssOffLoc() { super(); } public Office getOffice() { return office; } public void setOffice(Office office) { this.office = office; } public Local getLocal() { return local; } public void setLocal(Local local) { this.local = local; } @Override public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; AssOffLoc assOffLoc = (AssOffLoc) o; return Objects.equals(office, assOffLoc.office) && Objects.equals(local, assOffLoc.local); } @Override public int hashCode() { return Objects.hash(office, local); } }

现在我需要为此表编写实体:

CREATE TABLE ASS_USER_OFF_LOC( USER NUMBER(19,0), OFFICE VARCHAR2(100), LOCAL VARCHAR2(100), CONSTRAINT ASS_USER_OFF_LOC_PK PRIMARY KEY (USER), CONSTRAINT SYS_C009555 CHECK ("USER" IS NOT NULL), CONSTRAINT SYS_C009556 CHECK ("OFFICE" IS NOT NULL), CONSTRAINT SYS_C009557 CHECK ("LOCAL" IS NOT NULL) ); ALTER TABLE ASS_USER_OFF_LOC ADD CONSTRAINT "ASS_USER_OFF_LOC_ASS_OFF_LOC_FK" FOREIGN KEY ("OFFICE", "LOCAL") REFERENCES "ASS_OFF_LOC" ("CODE_OFFICE", "CODE_LOCAL"); ALTER TABLE "ASS_USER_OFF_LOC" ADD CONSTRAINT "ASS_USER_OFF_LOC_USER_FK" FOREIGN KEY ("USER") REFERENCES "USER" ("USER_ID");

我如何为此表编写实体?该表仅允许同一用户具有一个记录,并且可以在办公室和本地的值中修改此记录。在实体中,我想使用用户实体以及ASSOFFLOC ENTITY

@Entity
@Table(name = "ASS_USER_OFF_LOC")
public class AssUserOffLoc implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @ManyToOne
    @JoinColumn(name = "USER", nullable = false)
    private User user;

    @Id
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name = "OFFICE", referencedColumnName = "CODE_OFFICE", nullable = false),
        @JoinColumn(name = "LOCAL", referencedColumnName = "CODE_LOCAL", nullable = false)
    })
    private AssOffLoc assOffLoc;
}

java spring hibernate spring-data-jpa
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.