开发者社区> 问答> 正文

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

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;
	}

}



请大神解答,感谢!!


展开
收起
爱吃鱼的程序员 2020-06-09 14:32:08 710 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

    从这里找到原因了。

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




    2020-06-09 14:32:24
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载