hibernate 注解多对一实体类时报错。

彩云小斯 发布于 2016/05/17 15:44
阅读 447
收藏 0

eclipse 通过hibernate tool 自动生成的实体类。

在测试时报错,信息如下:

Caused by: org.hibernate.MappingException: Could not determine type for: org.phone.manager.entity.ProductType, at table: product, for columns: [org.hibernate.mapping.Column(productType)]
	at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:396)
	at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:369)
	at org.hibernate.mapping.Property.isValid(Property.java:225)
	at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:529)
	at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
	at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
	at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:416)
	at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:401)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
	... 55 more



实体类如下

product类

package org.phone.manager.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "product", catalog = "phonemanager")
public class Product extends EntityParent {

	private ProductType productType;
	private Integer order;
	private String PName;
	private Float PPrice;
	private String notes;

	public Product() {
	}

	public Product(ProductType productType, Integer order, String PName, Float PPrice, String notes) {
		this.productType = productType;
		this.order = order;
		this.PName = PName;
		this.PPrice = PPrice;
		this.notes = notes;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "type_id")
	public ProductType getProductType() {
		return this.productType;
	}

	public void setProductType(ProductType productType) {
		this.productType = productType;
	}

	@Column(name = "order")
	public Integer getOrder() {
		return this.order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	@Column(name = "p_name", length = 45)
	public String getPName() {
		return this.PName;
	}

	public void setPName(String PName) {
		this.PName = PName;
	}

	@Column(name = "p_price", precision = 12, scale = 0)
	public Float getPPrice() {
		return this.PPrice;
	}

	public void setPPrice(Float PPrice) {
		this.PPrice = PPrice;
	}

	@Column(name = "notes", length = 200)
	public String getNotes() {
		return this.notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

}

productType

package org.phone.manager.entity;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "product_type", catalog = "phonemanager")
public class ProductType extends EntityParent {

	private String typeName;
	private Integer fatherId;
	private Integer order;
	private String description;
	private Set<Product> products = new HashSet<Product>(0);

	public ProductType() {
	}

	public ProductType(String typeName, Integer fatherId, Integer order, String description, Set<Product> products) {
		this.typeName = typeName;
		this.fatherId = fatherId;
		this.order = order;
		this.description = description;
		this.products = products;
	}

	@Column(name = "type_name", length = 45)
	public String getTypeName() {
		return this.typeName;
	}

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

	@Column(name = "father_id")
	public Integer getFatherId() {
		return this.fatherId;
	}

	public void setFatherId(Integer fatherId) {
		this.fatherId = fatherId;
	}

	@Column(name = "order")
	public Integer getOrder() {
		return this.order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	@Column(name = "description", length = 20)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "productType")
	public Set<Product> getProducts() {
		return this.products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}

}


实体类父类

package org.phone.manager.entity;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

@MappedSuperclass
public class EntityParent implements EntityInterface {
	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	@Version
	private Integer version;

	public EntityParent() {
	}

	public EntityParent(Integer id, Integer version) {
		this.id = id;
		this.version = version;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

}



请大神解答,感谢!!


加载中
0
彩云小斯
彩云小斯

http://stackoverflow.com/questions/6164123/org-hibernate-mappingexception-could-not-determine-type-for-java-util-set

从这里找到原因了。

hibernate 注解的地址要统一,都方法getter()方法上就行了。不要有的放在private的属性上,有的放在getter上,我父类的注解就在属性上,改到getter上问题解决了。。。




返回顶部
顶部