1

我正在使用 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_AUTHORBOOK_AUTHORAUTHOR表格中填充?一位作者既可以是序言作者,也可以是书籍作者,但每本书只能有一个序言作者和一个书籍作者。

如果有什么我可以澄清的,请告诉我。

感谢您的帮助!

/D

4

2 回答 2

4

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

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

@ManyToOne
@JoinColumn(name="BOOK_AUTHOR")
private Author bookAuthor;
于 2012-08-21T15:04:36.563 回答
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")

于 2021-05-18T06:55:16.730 回答