将表中的两列映射到另一个表中的同一列

时间:2012-08-21 14:58:27

标签: java spring hibernate mapping

我正在使用spring和hibernate创建一个Web应用程序。是否可以在一个表中引用多个@ManyToOne映射,以引用另一个表中的同一列?我的意思是

如果我有这两个表

@Entity
@Table(name="AUTHOR")
public class Author{

@Id
@GeneratedValue
@Column(name="AUTHOR_ID")   
private IntegerauthorId;

@Column(name="AUTHOR_NAME")
private String authorName;


...

}

@Entity
@Table(name="BOOK")
public class book{

@Id
@GeneratedValue
@Column(name="BOOK_ID")
private Integer bookId;


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

@Column(name="PREFACE_AUTHOR")
private Author prefaceAuthor;

@Column(name="BOOK_AUTHOR")
private Author bookAuthor;

...

}

如何进行映射,以便从PREFACE_AUTHOR表中填充BOOK_AUTHORAUTHOR? 一位作者可以是前言和书籍作者,但每本书只能有一位前言作者和一位书籍作者。

如果有任何我可以澄清的话,请告诉我。

感谢您的帮助!

/ d

2 个答案:

答案 0 :(得分:4)

您应该指定它是多对一的,并使用@JoinColumn代替@Column,如下所示:

@ManyToOne
@JoinColumn(name="PREFACE_AUTHOR")
private Author prefaceAuthor;

@ManyToOne
@JoinColumn(name="BOOK_AUTHOR")
private Author bookAuthor;

答案 1 :(得分:0)

我也有同样的情况。有两个实体:

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "book_id")
    int id;

    @Column(name = "book_name")
    String name;

    @ManyToOne
    @JoinColumn(name = "book_author", referencedColumnName = "person_id")
    Person author;

    @ManyToOne
    @JoinColumn(name = "book_reviewer", referencedColumnName = "person_id")
    Person reviewer;

    public Book(String name, Person author, Person reviewer) {
        this.name = name;
        this.author = author;
        this.reviewer = reviewer;
    }

    public Book() {
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author=" + author +
                ", reviewer=" + reviewer +
                '}';
    }
}

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "person_id")
    int id;

    @Column(name = "person_name")
    String name;

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

当我使用自定义查询创建 BookRepository 时

public interface BookRepository extends CrudRepository<Book, Integer> {
    List<Book> findAll();

    @Query("SELECT id, name, author, reviewer FROM Book")
    List<Book> testQuery();
}

抛出异常

Caused by: org.hibernate.QueryException: No data type for node:        org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'reviewer' {originalText=reviewer}
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:161) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:1026) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:794) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:694) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:330) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:278) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
... 73 common frames omitted

但是如果我删除第二个引用列

@Query("SELECT id, name, author FROM Book")
List<Book> testQuery();

程序无一例外地启动。

问题通过替换解决

@Query("SELECT id, name, author, reviewer FROM Book")

@Query("SELECT id, name, b.author, b.reviewer FROM Book b")