Hi I am working on spring boot and I have tried a lot of methods I keep getting this error but it did not happen and some code shows red can you helpenter image description here enter image description here enter image description here
CodePudding user response:
you need to configure the entity manager with username, password, and host. hibernate need entitymanager to connect database. you can add the configuration like this
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.password=root
spring.datasource.username=root
Also you need
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId> // your db connector
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
CodePudding user response:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.6.Final</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
</dependencies>
CodePudding user response:
public City(int id, String name, String countryCode, String district, int population) {
super();
this.id = id;
this.name = name;
this.countryCode = countryCode;
this.district = district;
this.population = population;
}
public City() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
CodePudding user response:
@Repository
public class HibernateCityDal implements ICityDal {
private EntityManager entityManager;
@Autowired
public HibernateCityDal(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
@Transactional
public List<City> getAll() {
Session session = entityManager.unwrap(Session.class);
List<City> cities = session.createQuery("from City",City.class).getResultList();
return cities;
}
@Override
public void add(City city) {
// TODO Auto-generated method stub
}
@Override
public void update(City city) {
// TODO Auto-generated method stub
}
@Override
public void delete(City city) {
// TODO Auto-generated method stub
}
}
CodePudding user response:
@Service public class CityManager implements ICityService {
private ICityDal cityDal;
@Autowired
public CityManager(ICityDal cityDal) {
this.cityDal = cityDal;
}
@Override
@Transactional
public List<City> getAll() {
return this.cityDal.getAll();
}
@Override
@Transactional
public void add(City city) {
}
@Override
@Transactional
public void update(City city) {
}
@Override
@Transactional
public void delete(City city) {
}
}
CodePudding user response:
@RestController
@RequestMapping("/api") public class CityController {
private ICityService cityService;
@Autowired
public CityController(ICityService cityService) {
this.cityService = cityService;
}
@GetMapping("/cities")
public List<City> get(){
return cityService.getAll();
}
}