테이블을 만들고 싶습니다. 여기서 id는 구성된 문자열입니다. 이것은 내가 몇 가지 예제 및 설명서 다음되는 한 방법입니다 전에합성 키 최대 절전 모드
@Entity
@Table(name = "media_locator")
public class MediaLocator {
private List<MediaObject> mediaObjects;
private MediaLocatorPK primaryKey = new MediaLocatorPK();
// @Id
// @GeneratedValue(strategy = GenerationType.AUTO)
// @Column(name = "id", unique = true, nullable = false)
// public int getId() {
// return id;
// }
//
// public void setId(int id) {
// this.id = id;
// }
@EmbeddedId
public MediaLocatorPK getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(MediaLocatorPK primaryKey) {
this.primaryKey = primaryKey;
}
// @Column(name = "location", length = 200)
public String getLocation() {
return primaryKey.getLocation();
}
public void setLocation(String location) {
this.primaryKey.setLocation(location);
}
// @Column(name = "description", length = 200, nullable = false)
public String getDescription() {
return primaryKey.getDescription();
}
public void setDescription(String description) {
this.primaryKey.setDescription(description);
}
// @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="mediaLocator")
// public List<MediaObject> getMediaObjects() {
// return mediaObjects;
// }
//
// public void setMediaObjects(List<MediaObject> mediaObjects) {
// mediaObjects = mediaObjects;
// }
@ManyToMany(
cascade={CascadeType.ALL},
fetch=FetchType.LAZY)
@JoinTable(name = "media_object_location",
[email protected](name="mediaLocator_id"),
[email protected](name="mediaObject_id"))
public List<MediaObject> getMediaObjects() {
return mediaObjects;
}
public void setMediaObjects(List<MediaObject> mediaObjects) {
this.mediaObjects = mediaObjects;
}
// @Column(name = "protocol", length = 200, nullable = false)
public String getProtocol() {
return primaryKey.getProtocol();
}
public void setProtocol(String protocol) {
this.primaryKey.setProtocol(protocol);
}
// @Column(name = "host", length = 200, nullable = false)
public String getHost() {
return primaryKey.getHost();
}
public void setHost(String host) {
this.primaryKey.setHost(host);
}
// @Column(name = "port", length = 200, nullable = false)
public String getPort() {
return primaryKey.getPort();
}
public void setPort(String port) {
this.primaryKey.setPort(port);
}
// @Column(name = "path", length = 200, nullable = false)
public String getPath() {
return primaryKey.getPath();
}
public void setPath(String path) {
this.primaryKey.setPath(path);
}
@Embeddable class MediaLocatorPK implements Serializable
{
private String location;
private String description;
private String protocol;
private String host;
private String port;
private String path;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
을, 나는 ID를 자동 생성했고, 각 atribute는 예를 들어, 테이블, @Column(name = "location", length = 200)
에 콜 럼을했다.
@ManyToMany(
cascade={CascadeType.ALL},
fetch=FetchType.LAZY)
@JoinTable(name = "media_object_location",
[email protected](name="mediaLocator_id"),
[email protected](name="mediaObject_id"))
public List<MediaObject> getMediaObjects() {
return mediaObjects;
}
public void setMediaObjects(List<MediaObject> mediaObjects) {
this.mediaObjects = mediaObjects;
}
그리고 미디어 오브젝트의 측면에서 : 인해 다른 개체
과의 관계에서 referencies이 방법은 내가 데이터를 저장할 수 없습니다에서Caused by: org.hibernate.AnnotationException: A Foreign key refering com.app.MediaLocator from com.app.MediaObject has the wrong number of column. should be 6
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:429)
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1443)
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1262)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:693)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:628)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1686)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
이 특정 관계는
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "mediaObjects")
public List<MediaLocator> getMediaLocators() {
return mediaLocators;
}
public void setMediaLocators(List<MediaLocator> mediaLocators) {
this.mediaLocators = mediaLocators;
}
내가 뭘 잘못하고있는거야? 당신은 메인 클래스에서 제거하는 데 필요한 임베드 클래스에 속성을 이동하면 귀하의 예제에서 모든를 제거해야하므로 사전에
덕분에
는
예외가 있습니까? – mprabhat
방금 제 질문을 편집했는데, 충분한 정보가 없다는 것을 깨달았습니다 –
좋은 디자인 (단일 ID 열, 자동 생성 됨)에서 나쁜 열 (모든 열이 복합 기본 키를 형성하는 테이블)로갔습니다. 왜 그것을하고 있습니까? MediaLocator 객체를 참조하는 모든 엔티티는 자체 테이블에 6 개의 열을 복제해야합니다. 그리고 MediaLocator에서 값을 변경할 수 없습니다. 그렇게하지 마! –