2014-09-25 55 views
2

嗨,我得到一些映射異常,請按照下面的錯誤如何糾正這種映射異常(@OneToMany或@ManyToMany的使用目標未映射類)

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.cmr.daos.child.domain.Child.medications[com.cmr.daos.child.domain.Medications] 
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1185) 
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:710) 
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:645) 
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65) 
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423) 
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) 
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:720) 
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) 
    ... 62 more 

我的域類:

public class Child extends AuditProperties implements java.io.Serializable { 

@Expose private Long childId; 
@Expose private String firstName; 
@Expose private String lastName; 

private Set<Allergies> allergies = new HashSet<Allergies>(); 

private Set<Medications> medications = new HashSet<Medications>(); 

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "child") 
@JsonManagedReference 
public Set<Medications> getMedications() { 
    return this.medications; 
} 

public void setMedications(Set<Medications> medications) { 
    this.medications = medications; 
} 

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "child") 
@JsonManagedReference 
public Set<Allergies> getAllergies() { 
    return this.allergies; 
} 

public void setAllergies(Set<Allergies> allergies) { 
    this.allergies = allergies; 
} 
@Id 
@GeneratedValue(strategy = IDENTITY) 
@Column(name = "CHILD_ID", unique = true, nullable = false) 
public Long getChildId() { 
    return this.childId; 
} 

public void setChildId(Long childId) { 
    this.childId = childId; 
} 
@Column(name = "FIRST_NAME", nullable = false, length = 64) 
public String getFirstName() { 
    return this.firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

@Column(name = "LAST_NAME", nullable = false, length = 64) 
public String getLastName() { 
    return this.lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

}

Here my mapped classs: 

public class Medications extends AuditProperties implements java.io.Serializable{ 
@Expose private Long medicationId; 
@Expose private String hasMedication; 
@Expose private String medicationType; 
@Expose private transient Child child; 

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
@JoinColumn(name = "CHILD_ID") 
@JsonBackReference 
public Child getChild() { 
    return child; 
} 


public void setChild(Child child) { 
    this.child = child; 
} 
@Id 
@GeneratedValue(strategy = IDENTITY) 
@Column(name = "MEDICATION_ID", unique = true, nullable = false) 
public Long getMedicationId() { 
    return medicationId; 
} 


public void setMedicationId(Long medicationId) { 
    this.medicationId = medicationId; 
} 

@Column(name = "HAS_MEDICATION", nullable = false, length = 3) 
public String getHasMedication() { 
    return hasMedication; 
} 


public void setHasMedication(String hasMedication) { 
    this.hasMedication = hasMedication; 
} 

@Column(name = "MEDICATION_TYPE", length = 64) 
public String getMedicationType() { 
    return medicationType; 
} 


public void setMedicationType(String medicationType) { 
    this.medicationType = medicationType; 
} 
} 

Here another mapped class: 
@Entity 
@Table(name = "ALLERGIES") 
public class Allergies extends AuditProperties implements java.io.Serializable { 
@Expose private Long allergyId; 
@Expose private String hasAllergies; 
@Expose private String allerigyType; 
@Expose private transient Child child; 

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
@JoinColumn(name = "CHILD_ID") 
@JsonBackReference 
public Child getChild() { 
    return child; 
} 

public void setChild(Child child) { 
    this.child = child; 
} 

@Id 
@GeneratedValue(strategy = IDENTITY) 
@Column(name = "ALLERGY_ID", unique = true, nullable = false) 
public Long getAllergyId() { 
    return allergyId; 
} 
public void setAllergyId(Long allergyId) { 
    this.allergyId = allergyId; 
} 

@Column(name = "HAS_ALLERGIES", length = 3) 
public String getHasAllergies() { 
    return hasAllergies; 
} 
public void setHasAllergies(String hasAllergies) { 
    this.hasAllergies = hasAllergies; 
} 

@Column(name = "ALLERIGY_TYPE", length = 20) 
public String getAllerigyType() { 
    return allerigyType; 
} 
public void setAllerigyType(String allerigyType) { 
    this.allerigyType = allerigyType; 
} 

}

這裏我提到了一個孩子班,過敏類和藥物班。在這裏我將孩子對象映射到兩個類(過敏,藥物),然後我會得到這個異常。請幫助我abot此例外

+2

發佈課程及其註釋。不僅僅是它們包含的方法和字段。 – 2014-09-25 06:40:46

+1

這不是應該問的問題。 您將通過使用Google搜索得到答案。 – 2014-09-25 12:07:12

回答

5

作爲例外說:

Use of @OneToMany or @ManyToMany targeting an unmapped class: 
com.cmr.daos.child.domain.Child.medications[com.cmr.daos.child.domain.Medications] 

休眠正在試圖尋找代表在Child類物業medications實體Medications

看着一切看起來不錯,所以我假設你錯過了@EntityMedications類,或者你錯過了在hibernate.cfg.xml文件中提及這個實體。

+0

Thnaks,現在我得到了點 – HariKrishna 2014-09-25 06:53:52

+0

@HariKrishna,那麼你的情況是什麼問題? – Chaitanya 2014-09-25 06:54:36

+1

像@Entity @Table(name =「MEDICATION」)這樣的bean類錯過了註釋現在我添加了這個,然後它的工作正常 – HariKrishna 2014-09-25 07:00:16

相關問題