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

时间:2019-01-21 19:11:54

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

非临时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和getters,在这里我没有为了缩短代码而添加它们。

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

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

1 个答案:

答案 0 :(得分:0)

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