为什么JPA会为错误的表执行delete语句?

时间:2017-07-02 13:15:04

标签: java sql oracle jpa

我正在使用Java EE编写一个简单的论坛。论坛有线程和帖子实体。帖子和线程的所有基本CRUD操作都在工作。

只有当我尝试删除一个线程时,JPA才会生成错误的SQL语句,我不明白为什么。我正在使用Oracle 11g数据库。

JPA的错误陈述:

import { CommonModule } from "@angular/common"

应该如何:

delete from THREAD_POST where Thread_id=?

看起来JPA正在尝试在线程和帖子之间使用连接表。 希望完全有人可以指出我正确的方向来解决这个错误。

我执行删除线程的代码:

delete from FORUM.THREAD where Thread.id=?

My Thread实体:

@PersistenceContext
private EntityManager em;

em.remove(em.find(Thread.class, id));

我的邮政实体:

package ee.forum.ejb.model;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(schema = "FORUM", name = "THREAD")
public class Thread implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(
            name = "THREAD_ID_GENERATOR",
            sequenceName = "SEQ_THREAD",
            schema = "FORUM",
            allocationSize = 1,
            initialValue = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "THREAD_ID_GENERATOR"
    )
    private Long id;

    @NotNull
    @Size(max = 20)
    private String name;

    @NotNull
    @ManyToOne
    private Board board;

    @NotNull
    @ManyToOne
    private User author;

    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;

    @OneToMany
    private List<Post> posts;

    public Thread() {}

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Board getBoard() {
        return board;
    }

    public void setBoard(Board board) {
        this.board = board;
    }

    public User getAuthor() {
        return author;
    }

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

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public List<Post> getPosts() {
        return posts;
    }

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

    public boolean addPost(Post post) {
        if (posts == null) {
            posts = new ArrayList<>();
        }
        post.setThread(this);
        return this.posts.add(post);
    }
}

0 个答案:

没有答案