Answer the question
In order to leave comments, you need to log in
Spring + JPA how to make queries work?
Good afternoon. Requests don't work. findAll works, custom queries don't.
application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/ticket_system
spring.datasource.driver-class-name=org.gjt.mm.mysql.Driver
spring.datasource.username=root
@Repository
public interface CitiesRepo extends CrudRepository<City, Integer> {
List<City> findByName(String name);
}
List<City> cities = citiesRepo.findByName(text);
Hibernate: select city0_.id as id1_0_, city0_.latitude as latitude2_0_, city0_.longitude as longitud3_0_, city0_.name as name4_0_, city0_.region as region5_0_ from cities city0_ where city0_.name=?
@Entity
@Table(name = "cities")
public class City {
@Id
@SequenceGenerator(name="newRec", sequenceName="CITIES_SEQ",allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true,nullable = false)
private Integer id;
@Column(name = "name", length = 50,nullable = false)
private String name;
@Column(name = "region", length = 50,nullable = false)
private String region;
@Column(name = "latitude")
private float latitude;
@Column(name = "longitude")
private float longitude;
Answer the question
In order to leave comments, you need to log in
Good afternoon!
- Specify which libraries you have connected in pom.xml? In particular, the spring data jpa library is of interest
- Check if you have getters, setters and a constructor without arguments in the City class? If not, then add them.
If you have lombok connected, then you can write @Data @NoArgsConstructor
- Next, connect to your database and view the records in the database. It is possible that they are not stored in UTF-8, which is why the city is not found by name. Instead of data in the table, you will see kryakozyabry. If this problem is observed, then write
jdbc:mysql://localhost:3306/ticket_system?useUnicode=yes&characterEncoding=UTF-8
@Column(name = "`name`", length = 50,nullable = false)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question