2017-01-06 57 views
0

我試圖在產品規範中使用onetomany映射,其中從說明中複製了類似的工作代碼。我提供了我所有的代碼。org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany以未映射的類爲目標

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: sm-unit] Unable to build EntityManagerFactory 
     at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915) 
     at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890) 
     at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74) 
     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257) 
     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) 
     ... 38 more 
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.salesmanager.core.business.catalog.product.model.Product.specifications[com.salesmanager.core.business.catalog.product.model.specification.ProductSpecification] 
     at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1204) 

Product.java有記入這樣的:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product") 
    private Set<ProductSpecification> specifications = new HashSet<ProductSpecification>(); 

ProductSpecification.java低於代碼..

@Entity 
@Table(name = "PRODUCT_SPECIFICATION", schema=SchemaConstant.BEAWLP_JAMBIRA_SCHEMA) 
public class ProductSpecification extends SalesManagerEntity<Long, ProductSpecification> { 
    private static final long serialVersionUID = -7991123525661321865L; 

    @ManyToOne(targetEntity = Product.class) 
    @JoinColumn(name = "PRODUCT_ID", nullable = false) 
    private Product product; 

@Id 
@Column(name = "SPECIFICATION_ID", unique = true, nullable = false) 
@TableGenerator(name = "TABLE_GEN", table = "SM_SEQUENCER", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "SPECIFICATION_SEQ_NEXT_VAL") 
@GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN") 
private Long id; 

@Column(name="TITLE", length=100) 
private String title; 

public ProductSpecification() { 
} 

public Product getProduct() { 
    return product; 
} 

public void setProduct(Product product) { 
    this.product = product; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 
+0

嘗試'@OneToMany(...,targetEntity = ProductSpecification)'在你的註釋 – ZeusNet

+0

感謝ZeusNet。 \t 即使添加targetEntity後,我也會得到同樣的例外 – user3338029

+0

您是否也可以提供YouTube產品類的源代碼? – ZeusNet

回答

1

從錯誤消息中可以知道實體沒有正確映射。

  • 確保您已導入javax.persistence.entity而不是org.hibernate.annotations.entity
  • 另外,確保實體在配置文件(persistence.xml,hibernate.cfg.xml)中列出。
+0

是的。謝謝蘇雷什。我在persistence.xml中輸入了ProductSpecification條目的拼寫錯誤 – user3338029

+0

很高興能提供幫助。 –

0

我想你錯過targetEntity,嘗試下面一行它可能幫助你。

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product", targetEntity = ProductSpecification.class) 
private Set<ProductSpecification> specifications = new HashSet<ProductSpecification>(); 
+0

即使添加targetEntity後,我也會得到相同的異常 – user3338029

0

我注意到使用Set和HashSet時發生異常。您可以改用名單和ArrayList

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product") 
private List<ProductSpecification> specifications = new ArrayList<ProductSpecification>(); 
相關問題