gpt4 book ai didi

java - @OneToMany 与@JoinTable 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:37:16 24 4
gpt4 key购买 nike

我正在尝试用 **@JoinTable** 来理解 **@OneToMany**

enter image description here

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

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

我做错了什么?谁能帮我理解一下?

数据链接:

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);
}
}

用户模型:

@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;
}
}

订单模型:

@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 - @OneToMany 与@JoinTable 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34351803/

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