java - 为什么此设置不适用于 JPA 和 Hibernate 中的外键关系?

标签 java hibernate jpa

我正在尝试与 JPA 建立外键关系注释并使用 Hibernate 生成表格,但我收到错误并且看不到其原因。我执行 User 中的 main 方法来生成表。

这是错误 -

INFO: Hibernate Validator not found: ignoring
Exception in thread "main" org.hibernate.MappingException: Could not determine type    for: com.site.model.Name, at table: USER, for columns: [org.hibernate.mapping.Column(name)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.Column.getSqlTypeCode(Column.java:164)
at org.hibernate.mapping.Column.getSqlType(Column.java:208)
at org.hibernate.mapping.Table.sqlCreateString(Table.java:418)
at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:1099)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:131)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:92)
at com.thepoliticalbottomline.model.User.main(User.java:55)

以及源代码,

package com.sitename.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
public class User {
    private Long id;
    private String password;
    private Name name;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setName(Name name) {
        this.name = name;
    }
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public Name getName() {
        return name;
    }

    public static void main(String args[]) {
        Configuration config = new Configuration();
        config.addAnnotatedClass(User.class);
        config.addAnnotatedClass(Name.class);
        config.configure();
        new SchemaExport(config).create(true, true);
    }
}

类名,

package com.sitename.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Name {
    private Long id;
    private String first;
    private String middle;
    private String last;

    /**
     * @return the id
     */
    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    /**
    * @param id the id to set
    */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the first
     */
    public String getFirst() {
        return first;
    }

    /**
     * @param first the first to set
     */
    public void setFirst(String first) {
        this.first = first;
    }

    /**
     * @return the middle
     */
    public String getMiddle() {
        return middle;
    }

    /**
     * @param middle the middle to set
     */
    public void setMiddle(String middle) {
        this.middle = middle;
    }

    /**
     * @return the last
     */
    public String getLast() {
        return last;
    }

    /**
     * @param last the last to set
     */
    public void setLast(String last) {
        this.last = last;
    }
}

最佳答案

您不能在属性和方法上混合使用 JPA 注释。您只能注释给定类中的属性方法。

这适用于 hibernate 。我不确定其他 JPA 实现。

关于java - 为什么此设置不适用于 JPA 和 Hibernate 中的外键关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240069/

相关文章:

java - 如何使用 JPA 将索引集合映射到列表?

java - 在java中使用POI jar读取xlsx文件时出现异常

java - 使用 gcc 编译时未创建输出文件

java - 在 Spring Security 中使用个人资料名称或电子邮件登录

java.lang.ClassCastException : org. apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper 无法转换为 oracle.jdbc.OracleConnection

java - 单个查询能否给出 Spring Boot 中条件结果的结果?

java 。来自 URL 的损坏图片

java - Restful Web 服务字符串响应

java - HQL 无法理解一个类的属性是另一个类的集合

java - [错误]org.springframework.beans.factory.BeanCreationException