누군가가 최대 절전 모드에서 MappingBy를 설명 할 수 있습니까?
나는 최대 절전 모드를 사용하기 때문에 일대 다 관계와 다 대일 관계를 사용해야합니다. 객체에서 양방향 관계이므로 어느 방향에서든 통과 할 수 있습니다. mappedBy
그러나 권장되는 방법이지만 이해할 수 없습니다. 누군가 설명 할 수 있습니까?
- 그것을 사용하는 것이 권장되는 방법은 무엇입니까?
- 어떤 목적으로 해결됩니까?
내 예제를 위해 주석이있는 클래스는 다음과 같습니다.
Airline
많은 소유AirlineFlights
- 많은 사람들
AirlineFlights
이 하나 에 속합니다Airline
항공사 :
@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를 소유자 / 식별 기관으로 만드는 것입니까?
이론적으로는 AirlinesFlights의 항공사가되기를 바랍니다.
By specifying the @JoinColumn
on both models you don't have a two way relationship. You have two one way relationships, and a very confusing mapping of it at that. You're telling both models that they "own" the IDAIRLINE column. Really only one of them actually should! The 'normal' thing is to take the @JoinColumn
off of the @OneToMany
side entirely, and instead add mappedBy to the @OneToMany
.
@OneToMany(cascade = CascadeType.ALL, mappedBy="airline")
public Set<AirlineFlight> getAirlineFlights() {
return airlineFlights;
}
That tells Hibernate "Go look over on the bean property named 'airline' on the thing I have a collection of to find the configuration."
MappedBy signals hibernate that the key for the relationship is on the other side.
This means that although you link 2 tables together, only 1 of those tables has a foreign key constraint to the other one. MappedBy allows you to still link from the table not containing the constraint to the other table.
mappedby
speaks for itself, it tells hibernate not to map this field. it's already mapped by this field [name="field"].
field is in the other entity (name of the variable in the class not the table in the database)
..
If you don't do that, hibernate will map this two relation as it's not the same relation
so we need to tell hibernate to do the mapping in one side only and co-ordinate between them.
mappedby="object of entity of same class created in another class”
Note:-Mapped by can be used only in one class because one table must contain foreign key constraint. if mapped by can be applied on both side then it remove foreign key from both table and without foreign key there is no relation b/w two tables.
Note:- it can be use for following annotations:- 1.@OneTone 2.@OneToMany 3.@ManyToMany
Note---It cannot be use for following annotation :- 1.@ManyToOne
In one to one :- Perform at any side of mapping but perform at only one side . It will remove the extra column of foreign key constraint on the table on which class it is applied.
For eg . If we apply mapped by in Employee class on employee object then foreign key from Employee table will be removed.
You started with ManyToOne mapping , then you put OneToMany mapping as well for BiDirectional way. Then at OneToMany side (usually your parent table/class), you have to mention "mappedBy" (mapping is done by and in child table/class), so hibernate will not create EXTRA mapping table in DB (like TableName = parent_child).
참고URL : https://stackoverflow.com/questions/9108224/can-someone-explain-mappedby-in-hibernate
'Programming' 카테고리의 다른 글
Pandas 데이터 프레임에서 특이 값 탐지 및 제외 (0) | 2020.06.02 |
---|---|
startActivity ()에서 번들을 전달 하시겠습니까? (0) | 2020.06.02 |
OSX의 Xcode를 최신 버전으로 어떻게 업데이트합니까? (0) | 2020.06.02 |
JAX-WS로 XML 요청 / 응답 추적 (0) | 2020.06.02 |
C #에서 목록을 문자열로 변환 (0) | 2020.06.02 |