Hibernate - kolumna nie istnieje

0

Cześć,
Piszę swoją pierwszą aplikację z wykorzystaniem Hibernate i utknąłem w miejscu ponieważ dostaje błąd:

Column "INTERN0_.MANAGER_ID" not found; SQL statement:

Klasa Intern:

@Entity
@Table(name = "managers")
public class Intern implements Serializable{
	@Id
    @GeneratedValue(generator = "inc")
    @GenericGenerator(name = "inc", strategy = "increment")
    @Column(name="id")
    private Integer id;
	@ManyToOne
	@JoinColumn(name = "authority_id")
	Authorities authorities;
	@ManyToOne
	@JoinColumn(name = "manager_id")
	Manager manager;
	
	@SuppressWarnings("unused")
	Intern(){
	}

	public Integer getId() {
		return id;
	}

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

	public Authorities getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Authorities authorities) {
		this.authorities = authorities;
	}

	public Manager getManager() {
		return manager;
	}

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

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Intern other = (Intern) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}

Tabela w bazie:

create table interns (
	id int unsigned primary key auto_increment,
	authority_id int unsigned,
	manager_id int unsigned,
	constraint fk_interns_authority_id foreign key(authority_id) references authorities(id),
	constraint fk_interns_manager_id foreign key(manager_id) references managers(authority_id)
);

Kod rzucający błędem:

@Repository
public interface InternRepository extends JpaRepository<Intern, Integer>{
	@Query("select i from Intern i where authority_id = ?1")
	List <Intern> findIntersByManagerId(Integer managerId);
}

Dopiero się uczę tej technologii więc proszę o wyrozumiałość.
Dziękuję.

0

Zakładam, że tabele i kolumny istnieją w bazie. Zauważ też, że nazwa metody a zapytanie są różne - szukasz po managerach czy po authority? Masz 2 wyjścia:

  1. Korzystasz ze Spring Data i to zapytanie w @Query jest niepotrzebne. Zmień nazwę metody na „findByManagerId” lub „findByAuthoritiesId” - zadziała automagicznie.
  2. Korzystasz z tego @Query, ale zapytanie robisz nie w SQL, tylko JPQL, czyli pewnie „from Intern i where i.manager.id = :managerId”

Wiem, że się uczysz. Dobra praktyką jest czytanie dokumentacji, proszę: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

0
Charles_Ray napisał(a):

Zakładam, że tabele i kolumny istnieją w bazie. Zauważ też, że nazwa metody a zapytanie są różne - szukasz po managerach czy po authority? Masz 2 wyjścia:

  1. Korzystasz ze Spring Data i to zapytanie w @Query jest niepotrzebne. Zmień nazwę metody na „findByManagerId” lub „findByAuthoritiesId” - zadziała automagicznie.
  2. Korzystasz z tego @Query, ale zapytanie robisz nie w SQL, tylko JPQL, czyli pewnie „from Intern i where i.manager.id = :managerId”

Wiem, że się uczysz. Dobra praktyką jest czytanie dokumentacji, proszę: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

Niestety w obu przypadkach dostaje ten sam błąd ;/

0

Coś mi tu nie pasuje: Intern mapuje się na tablicę managers, a pokazujesz tworzenie tabeli interns.
Może zmiana zmiana mapowania na prawidłową tabelę rozwiąże problem?
@Table(name = "managers") => @Table(name = "interns")

1 użytkowników online, w tym zalogowanych: 0, gości: 1