需要使用一对多和多对一关系。这是我对象中的双向关系,因此我可以从任一方向来回移动。mappedBy是推荐的解决方法,但是,我听不懂。有人可以解释:
mappedBy
推荐的使用方式是什么? 它解决什么目的? 就我的示例而言,这是带有注释的类:
Airline
AirlineFlights
irline
航空公司:
@Entity @Table(name="Airline") public class Airline { private Integer idAirline; private String name; private String code; private String aliasName; private Set<AirlineFlight> airlineFlights = new HashSet<AirlineFlight>(0); public Airline(){} public Airline(String name, String code, String aliasName, Set<AirlineFlight> flights) { setName(name); setCode(code); setAliasName(aliasName); setAirlineFlights(flights); } @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="IDAIRLINE", nullable=false) public Integer getIdAirline() { return idAirline; } private void setIdAirline(Integer idAirline) { this.idAirline = idAirline; } @Column(name="NAME", nullable=false) public String getName() { return name; } public void setName(String name) { this.name = DAOUtil.convertToDBString(name); } @Column(name="CODE", nullable=false, length=3) public String getCode() { return code; } public void setCode(String code) { this.code = DAOUtil.convertToDBString(code); } @Column(name="ALIAS", nullable=true) public String getAliasName() { return aliasName; } public void setAliasName(String aliasName) { if(aliasName != null) this.aliasName = DAOUtil.convertToDBString(aliasName); } @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL}) @JoinColumn(name="IDAIRLINE") public Set<AirlineFlight> getAirlineFlights() { return airlineFlights; } public void setAirlineFlights(Set<AirlineFlight> flights) { this.airlineFlights = flights; } }
航空公司航班:
@Entity @Table(name="AirlineFlight") public class AirlineFlight { private Integer idAirlineFlight; private Airline airline; private String flightNumber; public AirlineFlight(){} public AirlineFlight(Airline airline, String flightNumber) { setAirline(airline); setFlightNumber(flightNumber); } @Id @GeneratedValue(generator="identity") @GenericGenerator(name="identity", strategy="identity") @Column(name="IDAIRLINEFLIGHT", nullable=false) public Integer getIdAirlineFlight() { return idAirlineFlight; } private void setIdAirlineFlight(Integer idAirlineFlight) { this.idAirlineFlight = idAirlineFlight; } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="IDAIRLINE", nullable=false) public Airline getAirline() { return airline; } public void setAirline(Airline airline) { this.airline = airline; } @Column(name="FLIGHTNUMBER", nullable=false) public String getFlightNumber() { return flightNumber; } public void setFlightNumber(String flightNumber) { this.flightNumber = DAOUtil.convertToDBString(flightNumber); } }
编辑:
数据库模式:
AirlineFlights具有idAirline作为ForeignKey,而Airline没有idAirlineFlights。这使得AirlineFlights作为所有者/识别实体?
从理论上讲,我希望航空公司成为airlightFlights的所有者。
通过@JoinColumn在两个模型上都指定,你就没有双向关系。你有两个单向关系,并且在那个位置有一个非常混乱的映射。你要告诉这两个模型“拥有” IDAIRLINE列。实际上,实际上其中只有一个!“正常”的事情是完全@JoinColumn脱颖而出@OneToMany,而是将maptedBy添加到@OneToMany。
@JoinColumn
@OneToMany
maptedBy
@OneToMany(cascade = CascadeType.ALL, mappedBy="airline") public Set<AirlineFlight> getAirlineFlights() { return airlineFlights; }
这告诉Hibernate:“在我有一个用来查找配置的集合上,检查名为’airline’的bean属性。”