问题描述
我试图通过 ** @ JoinTable **
来了解 ** @ OneToMany **
这样的场景
I'm trying to understand **@OneToMany**
with **@JoinTable**
for such scenerio
我正在使用JPA 2.1,Hibernate 5.0.4和Oracle 11 XE.当我致电 userDao.save(user)
(下面的代码)时,我已经
I'm using JPA 2.1, Hibernate 5.0.4 and Oracle 11 XE. When i call userDao.save(user)
(code below) i've got
java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (HIBERNATE.USER2ORDER_PK) violated
我做错了什么?有人可以帮我理解吗?
What am I doing wrong? Could someone help me understand?
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:
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:
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;
}
}
推荐答案
合并表USERS2ORDERS的唯一约束应具有主键,作为字段user_id和order_id的组合.但是,建议将主键设置为record_id,但您不知道何时可以使用它.
The unique constraint in merging Table USERS2ORDERS should have a primary key as a combination of both fields user_id and order_id.However, it is recommended to have a primary key as record_id, you don't know when you can use it.
这篇关于@OneToMany与@JoinTable错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!