繁体   English   中英

在Spring Boot中未为同一对象设置标有@Transient批注的字段

[英]Fields marked with @Transient annotation are not being set for the same object in Spring Boot

非临时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端点时,霍尔会连接到剧院,并且当我在同一控制器中打印剧院对象时,剧院会反映为非null。

当我调用/addtheatrehall端点时,我得到了空指针异常,并且记录在日志中的Theater对象显示该剧院的/addtheatrehall值为null。

根据我在代码中的了解,您正在设置private List<TheatreHall> halls; @Transient ,在持久化到数据库时将其标记为忽略,因此它始终为null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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