java – @OneToMany @JoinTable错误

前端之家收集整理的这篇文章主要介绍了java – @OneToMany @JoinTable错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用** @ JoinTable **来理解** @ OneToMany **这样的场景

我正在使用JPA 2.1,Hibernate 5.0.4和Oracle 11 XE.当我调用userDao.save(用户)(下面的代码)时,我已经得到了

java.sql.sqlIntegrityConstraintViolationException: ORA-00001: unique constraint (HIBERNATE.USER2ORDER_PK) violated

我究竟做错了什么?有人可以帮我理解吗?

DDL:

DROP TABLE HIBERNATE.T_USER2ORDERS;
DROP TABLE HIBERNATE.T_ORDERS;
DROP TABLE HIBERNATE.T_USERS;
DROP SEQUENCE HIBERNATE.USERS_SEQ;
DROP SEQUENCE HIBERNATE.ORDERS_SEQ;

CREATE TABLE HIBERNATE.T_USERS (
  ID                                NUMBER(15),NAME                              VARCHAR2(100 CHAR),CONSTRAINT USER_PK PRIMARY KEY (ID)
);

CREATE TABLE HIBERNATE.T_ORDERS (
  ID                                NUMBER(15),ORDER_DETAILS                     VARCHAR2(100 CHAR) NOT NULL,CONSTRAINT UDETAILS_PK PRIMARY KEY (ID)
);

CREATE TABLE HIBERNATE.T_USER2ORDERS (
  ID_USER                           NUMBER(15) NOT NULL,ID_ORDER                          NUMBER(15) NOT NULL,CONSTRAINT USER2ORDER_PK PRIMARY KEY (ID_USER),CONSTRAINT USER_FK FOREIGN KEY (ID_USER) REFERENCES T_USERS(ID),CONSTRAINT ORDER_FK FOREIGN KEY (ID_ORDER) REFERENCES T_ORDERS(ID)
);

CREATE SEQUENCE HIBERNATE.USERS_SEQ START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;
CREATE SEQUENCE HIBERNATE.ORDERS_SEQ START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;

道:

@Repository
@Transactional
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    public static final Logger LOG = LoggerFactory.getLogger(UserDaoImpl.class);

    @Autowired
    public UserDaoImpl(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    @Override
    public Serializable save(UserModel user) {
        return getHibernateTemplate().save(user);
    }
}

的usermodel:

@Entity
@Table(name = "T_USERS")
@SequenceGenerator(name = "users-sequence-generator",sequenceName = "USERS_SEQ",initialValue = 0,allocationSize = 1)
public class UserModel {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "users-sequence-generator",strategy = GenerationType.SEQUENCE)
    private Long id;

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

    @OneToMany(
            cascade = CascadeType.ALL,orphanRemoval = true,fetch = FetchType.EAGER)
    @JoinTable(
        name="t_user2orders",joinColumns = {@JoinColumn(name="id_user",referencedColumnName="id")},inverseJoinColumns = {@JoinColumn(name="id_order",referencedColumnName="id")}
    )
    private Collection<OrderModel> orders = new LinkedHashSet<>();

    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 Collection<OrderModel> getOrders() {
        return orders;
    }

    public void setOrders(Collection<OrderModel> orders) {
        this.orders = orders;
    }
}

OrderModel:

@Entity
@Table(name="T_ORDERS")
@SequenceGenerator(name="orders-sequence-generator",sequenceName="ORDERS_SEQ",initialValue=0,allocationSize=1)
public class OrderModel {

    @Id
    @Column(name="id")
    @GeneratedValue(generator="orders-sequence-generator",strategy=GenerationType.SEQUENCE)
    private Long id;

    @Column(name="order_details")
    private String orderDetails;

    public Long getId() {
        return id;
    }

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

    public String getOrderDetails() {
        return orderDetails;
    }

    public void setOrderDetails(String orderDetails) {
        this.orderDetails = orderDetails;
    }
}

解决方法

感谢@tharindu_DG的建议,我发现了错误.

T_USER2ORDERS的唯一约束无效.

而CONSTRAINT USER2ORDER_PK PRIMARY KEY(ID_USER)应该是CONSTRAINT USER2ORDER_PK PRIMARY KEY(ID_ORDER)

谢谢你的帮助.

猜你在找的Java相关文章