gpt4 book ai didi

java - @ManyToOne 映射不适用于连接继承

转载 作者:搜寻专家 更新时间:2023-11-01 03:44:03 37 4
gpt4 key购买 nike

我有以下数据库结构:

CREATE TABLE `author` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`));

CREATE TABLE `message` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(500) NOT NULL,
`text` varchar(50000) NOT NULL,
`author_id` int(10) unsigned DEFAULT NULL,
`creation_date` datetime NOT NULL,
`last_update_date` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `author_id_fk` (`author_id`),
CONSTRAINT `message_ibfk_1` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`));

CREATE TABLE `comment` (
`id` int(10) unsigned NOT NULL,
`post_id` int(10) unsigned NOT NULL,
KEY `message_id_fk` (`id`),
KEY `post_id_fk` (`post_id`),
CONSTRAINT `comment_ibfk_1` FOREIGN KEY (`id`) REFERENCES `message` (`id`),
CONSTRAINT `comment_ibfk_2` FOREIGN KEY (`post_id`) REFERENCES `post` (`id`));

CREATE TABLE `post` (
`id` int(10) unsigned NOT NULL,
KEY `message_id_fk` (`id`),
CONSTRAINT `post_ibfk_1` FOREIGN KEY (`id`) REFERENCES `message` (`id`) ON DELETE CASCADE);

以及以下与 hibernate(3.5.4-Final) 的映射:

@Entity
@Table(name = "author")
public class Author {
private Long id = 0L;
private String name;
private String email;
private String password;
private Set<Post> posts;
private Set<Comment> comments;

@Id
@Column(name = "id")
@GeneratedValue
public Long getId() {
return id;
}

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

@Column(name = "name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Column(name = "email")
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Column(name = "password")
public String getPassword() {
return password;
}

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

@OneToMany(mappedBy = "author")
public Set<Post> getPosts() {
return posts;
}

public void setPosts(Set<Post> posts) {
this.posts = posts;
}

@OneToMany(mappedBy = "author")
public Set<Comment> getComments() {
return comments;
}

public void setComments(Set<Comment> comments) {
this.comments = comments;
}
}

@MappedSuperclass
@Table(name = "message")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Message implements Serializable {
private Long id;
private String title;
private String text;
private Author author;
private Date creationDate;
private Date lastUpdateDate;

@Id
@Column(name = "id")
@GeneratedValue
public Long getId() {
return id;
}

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

@Column(name = "title")
public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Column(name = "text")
public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

@ManyToOne
@JoinColumn(name = "author_id")
public Author getAuthor() {
return author;
}

public void setAuthor(Author author) {
this.author = author;
}

@Column(name = "creation_date")
public Date getCreationDate() {
return creationDate;
}

public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}

@Column(name = "last_update_date")
public Date getLastUpdateDate() {
return lastUpdateDate;
}

public void setLastUpdateDate(Date lastUpdateDate) {
this.lastUpdateDate = lastUpdateDate;
}
}

@Entity
@Table(name = "comment")
@PrimaryKeyJoinColumn(name="id")
public class Comment extends Message {
private static final long serialVersionUID = 1L;
private Post post;

@ManyToOne
@JoinColumn(name = "post_id")
public Post getPost() {
return post;
}

public void setPost(Post post) {
this.post = post;
}

}

@Entity
@Table(name = "post")
@PrimaryKeyJoinColumn(name="id")
public class Post extends Message {
private static final long serialVersionUID = 1L;
private Set<Comment> comments;

@OneToMany(mappedBy = "post")
public Set<Comment> getComments() {
return comments;
}

public void setComments(Set<Comment> comments) {
this.comments = comments;
}
}

主要思想是 Comment 和 Post 是从 Message 继承的,我希望它们都具有双向关系。但是当我运行以下代码时:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Author author = new Author();
author.setName("mike");
author.setPassword("123");
author.setEmail("mike@gmail.com");

Post post = new Post();
post.setAuthor(author);
post.setCreationDate(new Date());
post.setLastUpdateDate(new Date());
post.setText("Text");
post.setTitle("Title");

Long authorId = (Long)session.save(author);
Long postId = (Long)session.save(post);

tx.commit();

我收到以下错误:

ERROR JDBCExceptionReporter:101 - Unknown column 'author_id' in 'field list'
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [org.blogsample.mappingbeans.Post]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)

更新正如@JB Nizet 在我将 @MappedSuperclass 更改为 @Entity 之前提到的那样,之后我遇到了另一个错误 mappedBy reference an unknown target entity property: org.blogsample .mappingbeans.Comment.author,这是通过更改数据库结构解决的(从 message 表中删除 author_id,将其添加到每个 commentpost 并为此列创建了外键)并将 author(和带有映射的 getter/setter)移动到 CommentPost 类。

最佳答案

您的Message 类不应使用@MappedSuperclass 进行注释,而应使用@Entity 进行注释。 @MappedSuperclass 表示扩展此类的实体从父类(super class)继承列和关联,但这些列和关联进入子类的表。 author_id 不在 comment 表或 post 表中。它位于 message 表中。

此外,@Table 只能与实体一起使用。不适用于映射的父类(super class),它仅用于继承字段和关联,但不像实体那样映射到它自己的表。

关于java - @ManyToOne 映射不适用于连接继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7134351/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com