【问题标题】:Fields marked with @Transient annotation are not being set for the same object in Spring Boot在 Spring Boot 中没有为同一个对象设置带有 @Transient 注释的字段
【发布时间】:2019-06-15 05:22:45
【问题描述】:

非瞬态 JPA 字段不反映对象。

剧院实体类:-

@Entity(name = "Theatre")
public class Theatre {

    Theatre() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id")
    private long id;
    @Column(name = "name")
    @NotNull
    private String name;
    @Column(name = "address")
    @NotNull
    private String address;
    @Column(name = "city")
    @NotNull
    private String city;
    @Column(name = "is_active")
    @NotNull
    private Boolean isactive;

    public List<TheatreHall> getHalls() {
        return halls;
    }

    public void setHalls(List<TheatreHall> halls) {
        this.halls = halls;
    }

    //@Column(name="halls")
    //@OneToMany(mappedBy="theatre", cascade = CascadeType.ALL)
    @Transient
    //@JsonIgnore
    private List<TheatreHall> halls;
    @Transient
    @JsonIgnore
    private Map<Movie, LinkedList<Time>> map;

    //@JsonProperty(value="is_active")

    public Boolean getIsactive() {
        return isactive;
    }

    public void setIsactive(Boolean isactive) {
        this.isactive = isactive;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public long getId() {
        return id;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Map<Movie, LinkedList<Time>> getMap() {
        return map;
    }

    public void setMap(Map<Movie, LinkedList<Time>> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "Theatre{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", city='" + city + '\'' +
                ", isactive=" + isactive +
                ", halls=" + halls +
                ", map=" + map +
                '}';
    }

}

剧院控制器:-

@RestController
public class TheatreController {

    @Autowired
    private MovieRepository movierepository;
    @Autowired
    private TheatreRepository theatrerepository;

    @RequestMapping(value = "/getactivetheatres", method = RequestMethod.GET)
    public ResponseEntity getActiveTheatres(@RequestParam String city) {

        return new ResponseEntity(theatrerepository.findActiveTheatresByCity(city)
                ,
                HttpStatus.OK);
    }

    @RequestMapping(value = "/addtheatre", method = RequestMethod.POST)
    public HttpStatus addTheatre(@Valid @RequestBody Theatre theatre) {

        theatrerepository.save(theatre);
        //List<TheatreHall> list = new LinkedList<TheatreHall>();
        //theatre.setHalls(list);
        return HttpStatus.CRETED;

    }

    @RequestMapping(value = "/addtheatrehall", method = RequestMethod.PUT)
    public HttpStatus addHall(@RequestParam(value = "theatreid") long theatreid, @RequestBody TheatreHall theatreHall) {

        Theatre theatre = theatrerepository.findById(theatreid);
        System.out.println(theatre);
        if (theatre.getHalls() == null) {
            theatre.setHalls(new LinkedList<TheatreHall>());
        }
        theatre.getHalls().add(theatreHall);
        theatrerepository.save(theatre);
        System.out.println(theatre);
        return HttpStatus.ACCEPTED;

    }

    @RequestMapping(value = "/addmovie", method = RequestMethod.POST)
    public HttpStatus addMovie(@RequestParam(value = "theatreid") long theatreid, @RequestParam(value = "movieid") long movieid) {

        Theatre theatre = theatrerepository.findById(theatreid);
        Movie movie = movierepository.findMovieById(movieid);

        System.out.println(theatre);
        System.out.println(movie);
        for (TheatreHall hall : theatre.getHalls()) {
            if (hall.getMovie() == null) {
                hall.setMovie(movie);
                return HttpStatus.ACCEPTED;
            }

        }
        throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "All halls are occupied");
    }
}

剧院大厅:-

public class TheatreHall {
    private Theatre theatre;
    private Byte hallid;
    private Byte rows;
    private Byte columns;
    private boolean is_active;
    private Movie movie;
    private int vacant_seats;
    private boolean arr[][];
    Map<Byte, Byte> vacantseatscount;

    TheatreHall() {

    }

    TheatreHall(Byte hallid, Byte rows, Byte columns) {
        this.hallid = hallid;
        this.rows = rows;
        this.columns = columns;
        this.arr = new boolean[rows][columns];
        vacant_seats = rows * columns;
        vacantseatscount = new LinkedHashMap<Byte, Byte>();
        for (Byte i = 0; i < rows; i++) {
            vacantseatscount.put(i, columns);
        }
    }
}

TheatreHall 还包含 setter 和 getter,我没有在此处添加以缩短代码。

现在我面临的问题是,当我调用 /addtheatrehall 端点时,大厅会连接到剧院,当我在同一个控制器中打印剧院对象时,剧院大厅反映为非空。

当我调用/addtheatrehall 端点时,我得到空指针异常,并且打印在日志中的剧院对象显示该剧院的大厅值为空。

【问题讨论】:

  • 我对你在做什么有点困惑;您是否使用 PUT 请求调用 addtheatrehall 以尝试填充值,然后调用返回 null 的方法?
  • @ChrisTurner i) 我调用 /addtheatre 工作正常。 ii)我调用 /addtheatrehall 也可以。 iii) 我调用 /addmovie,它在 for 循环中将 null 指针作为 theatre.getHalls() 返回 null。我正在传递一个有效并存储在数据库中的电影 ID。请帮帮我。我被严重卡住了。

标签: java spring spring-boot spring-data-jpa hibernate-mapping


【解决方案1】:

从我在您的代码中可以看出,您将private List&lt;TheatreHall&gt; halls; 设置为@Transient,这在持久保存到数据库时将其标记为已忽略,因此它始终为空。

【讨论】:

  • 我不想在我的项目中使用 TheatreHall 作为实体。我可以在这里做些什么来使 /addtheatrehall 端点工作?
猜你喜欢
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
相关资源
最近更新 更多