如何解决hibernate错误:在实体映射中重复列?
嗨,我有以下型号:如何解决hibernate错误:在实体映射中重复列?
@Entity
class Flight{
private Airport airportFrom;
private Airport airportTo;
@OneToOne(fetch=FetchType.LAZY,optional=false)
public Airport getAirportFrom(){
return this.airportFrom;
}
@OneToOne(fetch=FetchType.LAZY,optional=false)
public Airport getAirportTo(){
return this.airportTo;
}
}
@Entity
class Airport{
private Integer airportId;
@Id
public Integer getAirportId(){
this.airportId;
}
}
而且我得到这个错误:
org.hibernate.MappingException: Repeated column in mapping for entity: model.entities.Flight column: airportId (should be mapped with insert="false" update="false")
它是您需要的@JoinColumn,而不是@Column。
@OneToOne(fetch=FetchType.LAZY,optional=false)
@JoinColumn(name="airportFrom", referencedColumnName="airportId")
public Airport getAirportFrom(){
return this.airportFrom;
}
等
(和Frotthowe提到的,它似乎有点奇怪了与机场被OneToOne航班。我必须承认,通常忽略了域名,并假设名称是一些伪无稽之谈方便的问题:))
我该怎么做这在XML映射(不是注释)? – TheVillageIdiot 2011-11-11 20:39:22
你Flight
类没有定义ID。一个实体有一个ID是正常的,我怀疑这可能与你的问题有关。
该类的Id它不是问题的一部分,这就是为什么它不在问题中。 – Neuquino 2010-11-21 00:58:30
@OneToOne
是错误的。这意味着每个机场只有一个航班。使用@ManyToOne
。而你需要通过@JoinColumn
我认为这取决于他的业务规则,但@JoinColumn是问题。 – 2015-09-04 10:03:15
使用@JoinColumn
与@OneToOne
一起指定引用往返于机场ID列还要注意的是懒惰不会在这种情况下工作。
它的工作原理可选= false设置。 – Affe 2010-11-20 22:01:54
我用这对我的表,而不是机场的我有城市:
class Tender implements java.io.Serializable {
//...
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "city")
public City getCity() {
return this.city;
}
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "tour_city")
public City getTourCity() {
return this.tourCity;
}
//...
}
City implements java.io.Serializable {
//...
@Id
@SequenceGenerator(name = "city_pkey", sequenceName = "city_uid_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "city_pkey")
@Column(name = "uid", unique = true, nullable = false)
public int getUid() {
return this.uid;
}
//...
}
我已经尝试过在每添加@Column(name = “airportFrom)和@Column(name =” 机场至“)@ OneToOne,但我得到这个错误:“@oncolon(s)不允许在@onetoone财产” – Neuquino 2010-11-20 21:34:31