gpt4 book ai didi

java - @OneToOne 与 Hibernate 共享主键用户 - 帐户的映射

转载 作者:行者123 更新时间:2023-11-30 02:50:43 25 4
gpt4 key购买 nike

我尝试带this example为我的情况而工作。 Primus 是用户,Secundus 是帐户。用户应与帐户共享主 key 。一切正常,直到我尝试级联持久化。:

User user = new User();
user.setName("Andy");
this.uDao.create(user);

没问题并且可以工作,但是......

User user = new User();
user.setName("Andy");
Account account = new Account();
account.setUsername("xyz");
user.setAccount(account);
this.uDao.create(user);

给出错误:

01:14:35,844 INFO  [stdout] (ServerService Thread Pool -- 80) Hibernate: insert into user (name) values (?)

01:14:35,860 INFO [stdout] (ServerService Thread Pool -- 80) Hibernate: insert into account (username, user_id) values (?, ?)

01:14:35,861 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (ServerService Thread Pool -- 80) SQL Error: 1452, SQLState: 23000
01:14:35,861 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (ServerService Thread Pool -- 80) Cannot add or update a child row: a foreign key constraint fails (`shitstorm`.`account`, CONSTRAINT `fk_account_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

我能做什么?我的错误是什么?我正在写硕士论文,数据库中有一些 1:1 关系,需要处理这个问题。让 Cascading.persist 发挥作用对我来说很重要。我尝试了很多教程并阅读了很多解释,但我无法处理这种情况。我在链接中发布的教程是我的最后一次尝试。该 aaplication 正在 Wildfly 8.2.1 上运行,非常感谢!

这是 SQL:

CREATE TABLE IF NOT EXISTS `shitstorm`.`user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`id`))

-- -----------------------------------------------------
-- Table `shitstorm`.`account`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `shitstorm`.`account` (
`user_id` INT(11) NOT NULL,
`username` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`user_id`),
INDEX `fk_account_user_idx` (`user_id` ASC),
CONSTRAINT `fk_account_user`
FOREIGN KEY (`user_id`)
REFERENCES `shitstorm`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)

这是我的实体:

@Entity
@Table(name="user")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private int id;

@Column(length=45)
private String name;

//bi-directional one-to-one association to Account
@OneToOne(cascade=CascadeType.PERSIST, mappedBy="user")
private Account account;

public User() {
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
if(this.account != null){
this.account.setUserId(id);
}
}

public String getName() {
return this.name;
}

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

public Account getAccount() {
return this.account;
}

public void setAccount(Account account) {
this.account = account;
if(account != null){
account.setUser(this);
}
}

}

   @Entity
@Table(name="account")
@NamedQuery(name="Account.findAll", query="SELECT a FROM Account a")
public class Account implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="user_id", unique=true, nullable=false)
private int userId;

@Column(length=45)
private String username;

//bi-directional one-to-one association to User
@OneToOne
@PrimaryKeyJoinColumn(name="user_id")
//@JoinColumn(name="user_id", nullable=false, insertable=false, updatable=false) -- was generated by JPA TOOLS
private User user;



public Account() {
}

public int getUserId() {
return this.userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

public User getUser() {
return this.user;
}

public void setUser(User user) {
this.user = user;
}

}

最佳答案

您可以在 JPA 2+ 中更简单地实现此目的,如下所示:

public class User{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private int id;

@OneToOne(cascade=CascadeType.PERSIST, mappedBy = "user")
private Account account;
}

public class Account{

@Id
@OneToOne
@JoinColumn(name = "user_id")
private User user;
}

参见:

https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships

关于java - @OneToOne 与 Hibernate 共享主键用户 - 帐户的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38732035/

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