最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Jpa entity model - Stack Overflow

programmeradmin3浏览0评论

I have these entities:

User

@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);
        }

    }

Now i need to write entity for this table:

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");

How can I write the entity for this table? The table allows to have only one record for the same user and this record can be modified in the values ​​of the office and the local. In the entity I would like to map the user column with the user entity and the office and local columns with the AssOffLoc entity

I have these entities:

User

@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);
        }

    }

Now i need to write entity for this table:

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");

How can I write the entity for this table? The table allows to have only one record for the same user and this record can be modified in the values ​​of the office and the local. In the entity I would like to map the user column with the user entity and the office and local columns with the AssOffLoc entity

Share Improve this question asked Mar 16 at 19:17 panikopaniko 731 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
@Entity
@Table(name = "ASS_USER_OFF_LOC")
public class AssUserOffLoc implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @OneToOne
    @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;
}
发布评论

评论列表(0)

  1. 暂无评论