gpt4 book ai didi

java - 我是 Spring Boot 新手,试图了解 Hibernate 映射,下面是我的代码

转载 作者:行者123 更新时间:2023-12-01 17:47:02 25 4
gpt4 key购买 nike

我得到:

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory;

nested exception is org.hibernate.MappingException: Could not determine type for: com.xib.assessment.model.Team, at table: agent, for columns: [org.hibernate.mapping.Column(team)]

下面是我的Agent.java

private Long id;
private String firstName;
private String lastName;
private String idNumber;


private Team team;

@Column(name="team")
public Team getTeam() {
return team;
}

public void setTeam(Team team) {
this.team = team;
}

@OneToOne
private Manager manager;

public Long getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}


public Manager getManager() {
return manager;
}

public void setManager(Manager manager) {
this.manager = manager;
}

public String getIdNumber() {
return idNumber;
}

public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}

团队.java

private Long teamId;    

public Long getTeamId() {
return teamId;
}

public void setTeamId(Long teamId) {
this.teamId = teamId;
}

private String name;

@Column(name="agent")
private Agent agent;

@Column(name="manager")
public Manager getManager() {
return manager;
}

public void setManager(Manager manager) {
this.manager = manager;
}

@OneToOne
Manager manager;

public Agent getAgent() {
return agent;
}

public Team() {
super();
}

public void setAgent(Agent agent) {
this.agent = agent;
}

public Team(Long teamId, String name, Agent agent) {
this.teamId = teamId;
this.name = name;
this.agent = agent;
}
public String getName() {
return name;
}

public void setName(String name) {
}

manager.java

@Id
@GeneratedValue
private Long managerId;
private Team team;

@Column(name="team")
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
public Manager(Long managerId, Team team, String firstName, String lastName, String idNumber) {
super();
this.managerId = managerId;
this.team = team;
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
}
public Manager() {
}
public String getFirstName() {
return firstName;
}
public Long getManagerId() {
return managerId;
}
public void setManagerId(Long managerId) {
this.managerId = managerId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
private String firstName;
private String lastName;
private String idNumber;

其中:一名经理可以管理多个团队,任何一个团队最多可以由 2 名经理管理。和:一名客服人员只能分配给一个团队并向一名经理报告。

如何使用hibernate来映射上面的内容,提前谢谢

最佳答案

这是团队经理的示例。请遵循代码风格

@Entity
class Manager {

@Id
@GeneratedValue
private Long id;

@OneToMany(mappedBy = "manager", fetch = FetchType.LAZY)
private List<Team> teams = new ArrayList<>();

}

@Entity
class Team {

@Id
@GeneratedValue
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
private Manager manager;

}

混合字段和方法映射

不要在字段和方法上混合映射注释。在这种情况下,您将出现不清楚的错误。

您还可以在这里学习一些

what is @JoinColumn and how it is used in Hibernate

最好使用控制台应用程序尝试映射并检查 Hibernate 生成的 SQL。您可以为此项目使用单元测试

https://github.com/v-ladynev/hibernate-experimental

关于java - 我是 Spring Boot 新手,试图了解 Hibernate 映射,下面是我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60852044/

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