0

and i am getting:

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)]

below is my 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;
}

team.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;

where :A manager can manage multiple teams, and any one team can be managed by at most 2 managers. and : An agent can be assigned to only one team and reports to one manager.

how do you use hibernate to map the above , thank you in advance

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
mr L
  • 26
  • 2
  • Your class definitions are not complete – Smutje Mar 25 '20 at 15:35
  • @Smutje that i understand my question was on mapping annotations i just inserted few parts of the code so that i dont just paste everything here i only wanted to show what mapping that i tried – mr L Mar 25 '20 at 16:07
  • Hibernate mappings has nothing to do with Spring Boot. That is just hibernate. \ – M. Deinum Mar 25 '20 at 16:07
  • Part of solving a problem is understanding a problem. Pasting thousands of lines of code without trying to narrow down a problem and asking for help does not help your understanding of things. So, try to narrow it down and post complete, runnable, compilable code so we can help and not only meaningless snippets. – Smutje Mar 25 '20 at 16:13
  • @Smutjie i understand my problem that i am messing up on mapping, the reason why i pasted a snippet is to show what i have tried and also showing the stacktrace, just like i see people on this platform being asked to show stacktrace and what they have tried , all i was asking the making part not anythin else, and it cant be runnable when the code is not compiling because of the same errors i am talking about – mr L Mar 25 '20 at 16:20

1 Answers1

0

This is an example for Team and Manager. Follow it with code style please

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

}

Mixed field and method mapping

Don't mix mapping annotations on fields and methods. You will have unclear errors in that case.

Also you can learn a bit here

what is @JoinColumn and how it is used in Hibernate

Better to experiment with mappings using console application and checking SQL generated by Hibernate. You can use unit tests from this project for that

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

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • @mrL You can accept and upvote the answer, if it is helpful. – v.ladynev Mar 25 '20 at 16:58
  • what causes such an exception for my above code: SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement – mr L Mar 25 '20 at 23:42
  • @mrL You have incorrect mapping in `Agent` class. You need to understand difference between columns and associations. Also don't mix field and method mapping. – v.ladynev Mar 26 '20 at 09:37