使用@onetomany映射

时间:2015-08-24 12:53:39

标签: java hibernate

我创建了三个实体类,其关系定义如下: 屏幕----->有很多ConfigurableRows ConfigurableRows ----->有很多席位。

当我试图保持Screen Class对象时,没有表正在创建。

我在这里附加我的实体类。

public class Screen {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int screenid;

private String screenName;

@OneToMany(targetEntity=ConfigurableRow.class, mappedBy="screenid", fetch=FetchType.LAZY,cascade= CascadeType.ALL)
private List<ConfigurableRow> rows;

public Screen(){
    rows = new ArrayList<ConfigurableRow>(10);
}

public Screen(int rowSize){
    rows = new ArrayList<ConfigurableRow>(rowSize);
}

public String getScreenName() {
    return screenName;
}

public void setScreenName(String screenName) {
    this.screenName = screenName;
}

public int getScreenid() {
    return screenid;
}

public void setScreenid(int screenid) {
    this.screenid = screenid;
}

public List<ConfigurableRow> getRows() {
    return rows;
}

public void setRows(List<ConfigurableRow> rows) {
    this.rows = rows;
}

}

public class ConfigurableRow implements List<Seat>, IConfigureRow , IRow  {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int rowId;
private int numberOfSeats;
private String rowName;
private String screenName;

private int screenid;

@ElementCollection
List<Seat> seats;

public ConfigurableRow(){
    this.numberOfSeats = 10;
    this.rowName = "Default";
    this.screenName = "Default";
    this.seats = new ArrayList<>(numberOfSeats);
}

public ConfigurableRow(int numberOfSeats,String screenName , String rowName){
    this.numberOfSeats = numberOfSeats;
    this.rowName = rowName;
    this.screenName = screenName;
    this.seats = new ArrayList<Seat>(numberOfSeats);
}



public int getRowId() {
    return rowId;
}

@ManyToOne(targetEntity = Screen.class,fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn (name="screenid",referencedColumnName="screenid",nullable=false,unique=false)
public int getScreenid() {
    return screenid;
}

public void setScreenid(int screenid) {
    this.screenid = screenid;
}

public List<Seat> getSeats() {
    return seats;
}

public void setSeats(List<Seat> seats) {
    this.seats = seats;
}

public String getRowName() {
    return rowName;
}

public String getScreenName() {
    return screenName;
}

@Override
public boolean add(Seat e) {
    return seats.add(e);
}

@Override
public boolean addAll(Collection<? extends Seat> c) {
    if(c.size() >= numberOfSeats){
        throw new IllegalArgumentException("Seat Limit Exceeds!!!!!");
    }
    else {
        return addAll(c);
    }
}

@Override
public void clear() {
    seats.clear();
}

@Override
public boolean contains(Object o) {
    return seats.contains(o);
}

@Override
public boolean containsAll(Collection<?> c) {
    return seats.containsAll(c);
}

@Override
public boolean isEmpty() {
    return seats.isEmpty();
}

@Override
public Iterator<Seat> iterator() {
    return seats.iterator();
}

@Override
public boolean remove(Object o) {
    return seats.remove(o);
}

@Override
public boolean removeAll(Collection<?> c) {
    return seats.removeAll(c);
}

@Override
public boolean retainAll(Collection<?> c) {
    return seats.removeAll(c);
}

@Override
public int size() {
    return seats.size();
}

@Override
public Object[] toArray() {
    return seats.toArray();
}

@Override
public <T> T[] toArray(T[] a) {
    return seats.toArray(a);
}

@Override
public boolean addSeat(Seat seat) {
    if(seats.size() >= numberOfSeats){
        throw new IllegalArgumentException("Seat Limit Exceeds!!!!!");
    } else{
        return add(seat);
    }
}

@Override
public boolean addSeat(Seat seat, int seatNumber) {
    if(seats.size() >= numberOfSeats){
        throw new IllegalArgumentException("Seat Limit Exceeds!!!!!");
    } else{
        add(seatNumber,seat);
        return true;
    }
}

@Override
public boolean addAll(int index, Collection<? extends Seat> c) {
    if(seats.size() + c.size() > numberOfSeats){
        throw new IllegalArgumentException();
    }
    else {
        return seats.addAll(index, c);
    }
}

@Override
public Seat get(int index) {
    return seats.get(index);
}

@Override
public Seat set(int index, Seat element) {
    return seats.set(index, element);
}

@Override
public void add(int index, Seat element) {
    seats.add(index, element);
}

@Override
public Seat remove(int index) {
    return seats.remove(index);
}

@Override
public int indexOf(Object o) {
    return seats.indexOf(o);
}

@Override
public int lastIndexOf(Object o) {
    return seats.lastIndexOf(o);
}

@Override
public ListIterator<Seat> listIterator() {
    return seats.listIterator();
}

@Override
public ListIterator<Seat> listIterator(int index) {
    return seats.listIterator(index);
}

@Override
public List<Seat> subList(int fromIndex, int toIndex) {
    return seats.subList(fromIndex, toIndex);
}


public int getNumberOfSeats() {
    return numberOfSeats;
}

public void setNumberOfSeats(int numberOfSeats) {
    this.numberOfSeats = numberOfSeats;
}

public List<Seat> getRowSet() {
    return seats;
}

public void setRowSet(List<Seat> rowSet) {
    this.seats = rowSet;
}

}

public class Seat implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 8083252956190536785L;

/*@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int seatid;*/

private int seatNumber;

private SeatType seatType;

public int getSeatNumber() {
    return seatNumber;
}

public void setSeatNumber(int seatNumber) {
    this.seatNumber = seatNumber;
}

public SeatType getSeatType() {
    return seatType;
}

public void setSeatType(SeatType seatType) {
    this.seatType = seatType;
}

}

我是hibernate的新手。请告诉我这里我做错了什么。

我的桌子现在正在创建。但没有任何价值观。因此外键冲突错误即将到来。

1 个答案:

答案 0 :(得分:0)

我认为你应该在getrows()方法之前加上onomanomany注释。 @OneToMany(targetEntity=ConfigurableRow.class, mappedBy="screenid", fetch=FetchType.LAZY,cascade= CascadeType.ALL) public List<ConfigurableRow> getRows() { return rows; }