【问题标题】:Hibernate could not determine typeHibernate 无法确定类型
【发布时间】:2016-11-02 19:31:26
【问题描述】:

我被这个错误困住了:

org.hibernate.MappingException: Could not determine type for: com.mywebsite.entity.Image, at table: User, for columns: [org.hibernate.mapping.Column(profilePhoto)]

我知道它被问了很多,但我坚持这个。它已经尝试了几个小时,但我找不到任何有用的东西......

这是我的两门课:

@Entity
public class Image extends com.mywebsite.entity.Entity{

    //System
    @Id
    @GeneratedValue
    private int id;
    [...]

}


@Entity
public class User extends com.mywebsite.entity.Entity{

    //System
    @Id
    @GeneratedValue
    private int id;
    [...]

    //Data
    private Image profilePhoto;    
    [...]

}

有人可以帮我吗?

【问题讨论】:

  • 这行之前没有注释吗? private Image profilePhoto;
  • 不,我没有“私人字符串密码;”之前
  • 可以提供hbm文件吗?

标签: java hibernate


【解决方案1】:

编辑:

试试这个:

@ManyToOne
@JoinColumn(name="profilephoto_id", foreignKey = @ForeignKey(name = "put_name_here_If_You_HaveAForeignKeyConstraint"))    
private Image profilePhoto;

原文:


来自the docs

实体的每个非静态非瞬态属性(字段或方法取决于访问类型)都被认为是持久的,除非您将其注释为 @Transient

所以 Hibernate 认为该字段来自数据库,并试图在您的表中找到该列。该列不存在并且它应该存在,在这种情况下,您应该有一个注释映射,例如@Column;或者它在数据库中不存在并且不应该存在,在这种情况下,您应该像文档建议的那样使用@Transient

【讨论】:

  • 所以我添加了@Column(name="profilephoto_id") 正如你所说,错误改变了一点:com.mywebsite.entity.Image,在表:用户,列:[org.hibernate .mapping.Column(profilephoto_id)]。我的表用户中的列名是“profilephoto_id”。
  • 我不必添加“foreignKey”,没有它就可以工作。我明白为什么需要 joincolumn 而不是为什么需要 manytoone ?
  • 就是声明实体之间的关联。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
相关资源
最近更新 更多