java—通过Map的持久属性的反射访问字段时出错

67up9zun  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(351)

我已经用一对多关系设置了我的数据库,在尝试注册用户(保存到数据库)之后,我得到了whitelabe错误页
“通过永久属性的反射访问字段[private java.lang.integer com.m2.cfg.domain.fakultet.id]时出错”
错误
接下来是一对多关系:一个fakultet(大学)可以有多个用户
法库尔特(大学)班

@Entity
@Table(name = "fakulteti")
public class Fakultet {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "naziv")
    private String naziv;

    public Fakultet(){};

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNaziv() {
        return naziv;
    }

    public void setNaziv(String naziv) {
        this.naziv = naziv;
    }
}

用户类别:

@Entity
@Table(name = "users")
public class Users {
    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id = 0;
    @Column(name = "username")
    private String username;
    @Column(name="email")
    private String email;
    @Column(name="password")
    private String password;
    @Column(name = "enabled")
    private boolean enabled = true;
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Fakultet.class)
    @JoinColumn(name="id_fakulteta", referencedColumnName = "id")
    private Integer idFakulteta;

    public Users(){};

    public Users(String username, String email, String password, Integer idFakulteta) {
        this.username = username;
        this.email = email;
        this.password = password;
        this.idFakulteta = idFakulteta;
    }

    ..getters and setters
}

sql脚本:

DROP TABLE IF EXISTS employee;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS authorities;
DROP TABLE IF EXISTS kolegiji;

CREATE TABLE employee (
      empId VARCHAR(10) NOT NULL,
      empName VARCHAR(100) NOT NULL
);

create table users (
       id INT AUTO_INCREMENT PRIMARY KEY,
       username varchar(255) not null ,
       email varchar(255),
       password varchar(255) not null,
       id_fakulteta int,
       enabled boolean
);

CREATE TABLE IF NOT EXISTS fakulteti (
     naziv VARCHAR(255) NULL,
     id INT NOT NULL,
     PRIMARY KEY (id),
     CONSTRAINT fk_fakulteti_users1
         FOREIGN KEY (id)
             REFERENCES users (id_fakulteta)
             ON DELETE NO ACTION
             ON UPDATE NO ACTION
);

create table authorities (
         username varchar(50) not null,
         authority varchar(50) not null,
         foreign key (username) references users (username)
);

CREATE TABLE kolegiji (
      id int NOT NULL AUTO_INCREMENT,
      naziv varchar(255) DEFAULT NULL,
      PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS teme (
     idteme INT NOT NULL AUTO_INCREMENT,
     naslov VARCHAR(255) NOT NULL,
     datumKreiranja DATE NOT NULL,
     kolegijId INT NULL,
     PRIMARY KEY (idteme),
     CONSTRAINT fk_teme_kolegiji1
         FOREIGN KEY (kolegijId)
             REFERENCES kolegiji (id)
             ON DELETE NO ACTION
             ON UPDATE NO ACTION
);
create INDEX fk_teme_kolegiji1_idx on teme(kolegijId ASC);

CREATE TABLE IF NOT EXISTS komentari (
      id INT NOT NULL AUTO_INCREMENT,
      idTeme INT NOT NULL,
      PRIMARY KEY (id),

      CONSTRAINT fk_komentari_teme
          FOREIGN KEY (idTeme)
              REFERENCES teme (idteme)
              ON DELETE NO ACTION
              ON UPDATE NO ACTION
);
create INDEX fk_komentari_teme_idx on komentari(idTeme ASC);

CREATE TABLE IF NOT EXISTS kolegiji_has_fakulteti(
   kolegiji_id INT NOT NULL,
   fakulteti_id INT NOT NULL,
   PRIMARY KEY (kolegiji_id, fakulteti_id),
   CONSTRAINT fk_kolegiji_has_fakulteti_kolegiji1
       FOREIGN KEY (kolegiji_id)
           REFERENCES kolegiji (id)
           ON DELETE NO ACTION
           ON UPDATE NO ACTION,
   CONSTRAINT fk_kolegiji_has_fakulteti_fakulteti1
       FOREIGN KEY (fakulteti_id)
           REFERENCES fakulteti (id)
           ON DELETE NO ACTION
           ON UPDATE NO ACTION
);
create INDEX fk_kolegiji_has_fakulteti_fakulteti1_idx on kolegiji_has_fakulteti(fakulteti_id ASC);
create INDEX fk_kolegiji_has_fakulteti_kolegiji1_idx on kolegiji_has_fakulteti(kolegiji_id ASC);

insert into users(username, password, enabled, id_fakulteta, email)
    values('javainuse', 'javainuse',true, 1,'nekimail@gmail.com');
insert into authorities(username,authority)values('javainuse','ROLE_ADMIN');

insert into fakulteti(naziv, id) values('TVZ', 1);
qvsjd97n

qvsjd97n1#

我犯的错误就在这一行

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Fakultet.class)
    @JoinColumn(name="id_fakulteta", referencedColumnName = "id")
    private Integer idFakulteta;

当连接列时,必须创建要连接的对象,并且行必须如下所示

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Fakultet.class, cascade=CascadeType.ALL)
    @JoinColumn(name="id_fakulteta", nullable = false)
    private Fakultet faks;

我还需要删除
整数idfakulteta
字段以避免将同一列Map两次出错

相关问题