Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未知实体:java.lang.String_Java_Spring_Hibernate - Fatal编程技术网

未知实体:java.lang.String

未知实体:java.lang.String,java,spring,hibernate,Java,Spring,Hibernate,我的小网络应用程序有问题 添加SQL connect后,我无法删除/添加/编辑我的汽车。这里怎么了? 我是入门级,所以请直接向我解释问题所在 package com.RentCar.Rent_A_car.domain; import javax.persistence.*; @Entity public class Car { // @Id // @GeneratedValue(strategy = GenerationType.AUTO) // //int

我的小网络应用程序有问题

添加SQL connect后,我无法删除/添加/编辑我的汽车。这里怎么了? 我是入门级,所以请直接向我解释问题所在

  package com.RentCar.Rent_A_car.domain;

    import javax.persistence.*;

@Entity
public class Car {


//    @Id
//    @GeneratedValue(strategy = GenerationType.AUTO)
//    //int id;
    String mark;
    @Id
    String plate;
    int mileage;

    @OneToOne
    public Errand errand;

    public Car() { }

    public Car(String mark, String plate, int mileage) {
        this.mark = mark;
        this.plate = plate;
        this.mileage = mileage;

    }


    public void setErrand(Errand errand) {
        this.errand = errand;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    public String getPlate() {
        return plate;
    }

    public void setPlate(String plate) {
        this.plate = plate;
    }

    public int getMileage() {
        return mileage;
    }

    public void setMileage(int mileage) {
        this.mileage = mileage;
    }

    public Errand getErrand() {
        return errand;
    }

    /*
        To jest sposób na wstryzkiwanie przez metodę


         */
    //    public Car(String mark, String plate, int mileage) {
//        this.mark = mark;
//        this.plate = plate;
//        this.mileage = mileage;
//    }
//
    @Override
    public String toString() {
        // dodać czy jest uszkodzone, stoi na parkingu, jest u mechanika
        if(errand == null){
            return "Car " + mark + ". Plate number: " + plate + ". Milage: " + mileage +
                    ". Errand of car : Car have no errand";
        } else {
            return "Car " + mark + ". Plate number: " + plate + ". Milage: " + mileage +
                    ". Current errand: " + errand;
        }
    }
}
这是我的卡雷波课

    enter code here
    package com.RentCar.Rent_A_car.domain.Repository;

import com.RentCar.Rent_A_car.domain.Car;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class CarsRepository {

    // class to do CRUD operation on cars and errands

    Map<String, Car> CarList = new HashMap<>();

    @PersistenceContext
    private EntityManager em;



    @Transactional
    public void AddCar (String mark, String plate, int mileage){
        Car newCar = new Car(mark, plate, mileage);
        em.persist(newCar);

    }
     //pobiera aktualne id i dodaje do mapy
    public void AddCar(Car car) {
        em.persist(car);

    }

    // dla każdego "powyzej" usuniętego trzeba zmniejszyć id o 1 ; pętla o dlugosci pozostałych    aut id--; KONIECZNE FINALNIE, do budowy nie
    @Transactional
    public void DellateCar(String plate){
        em.remove(plate);

    }


    public Car getCar(String plate){

        Car car = em.createQuery("from Car c where c.plate=:plate", Car.class)
                .setParameter("plate", plate).getSingleResult();
        return car;
    }

    public Collection<Car> getAllCars() {
        return em.createQuery("from Car",Car.class).getResultList();

    }
    @Transactional
    public void updateCar(Car car) {
        em.merge(car);
    }

//    public String getPlate() {
//        if (CarList.isEmpty()) {
//            return "Car list is empty";
//        } else {
//            return (String)CarList.keySet();
//        }
//    }

//    @PostConstruct
//    public void build() {
//        AddCar("BMW","DW",100000);
//        AddCar("Opel","DTR",100043);
//        AddCar("Toyota","WB",1012300);
//        AddCar("Audi","DST",102340);
//
//    }


    @Override
    public String toString() {

        return "CarsRepository{" +
                "CarList="  +
                '}' + "\n";
    }



}
在此处输入代码
包com.RentCar.Rent_A_car.domain.Repository;
导入com.RentCar.Rent_A_car.domain.car;
导入org.springframework.stereotype.Repository;
导入javax.persistence.EntityManager;
导入javax.persistence.PersistenceContext;
导入javax.transaction.Transactional;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.Map;
@存储库
公共类汽车储蓄{
//类,用于对汽车和差事执行CRUD操作
Map CarList=newhashmap();
@持久上下文
私人实体管理者;
@交易的
公共车辆(字符串标记、字符串板、整数里程){
新车=新车(标记、车牌、里程);
em.persist(新车);
}
//pobiera aktualne我是dodaje do mapy
公共车辆{
em.persist(car);
}
//在第1次会议上,我们讨论了“powyzej”一词;在第2次会议上,我们讨论了“pozosta”一词;在最后一次会议上讨论了“budowy nie”一词
@交易的
公共车辆(线板){
em.移除(板);
}
公共汽车(线板){
Car Car=em.createQuery(“来自Car c,其中c.plate=:plate”,Car.class)
.setParameter(“plate”,plate).getSingleResult();
返回车;
}
公共收集getAllCars(){
返回em.createQuery(“from Car”,Car.class).getResultList();
}
@交易的
公共空间更新卡(汽车){
em.merge(car);
}
//公共字符串getPlate(){
//if(CarList.isEmpty()){
//返回“车辆列表为空”;
//}其他{
//返回(字符串)CarList.keySet();
//        }
//    }
//@施工后
//公共void build(){
//AddCar(“宝马”、“DW”,100000);
//AddCar(“欧宝”、“DTR”,100043);
//AddCar(“丰田”,“WB”,1012300);
//AddCar(“奥迪”,“DST”,102340);
//
//    }
@凌驾
公共字符串toString(){
返回“CarsRepository{”+
“卡利斯特=”+
“}”+“\n”;
}
}
这是我的控制器类

   package com.RentCar.Rent_A_car.domain.Controllers;

import com.RentCar.Rent_A_car.domain.Car;
import com.RentCar.Rent_A_car.domain.Repository.CarsRepository;
import com.RentCar.Rent_A_car.domain.Services.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.transaction.Transactional;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@Controller
    public class CarController {

// nie powinno się komunikować między bazą danych a kontrolerem, pomiędzy powinien być serwis.
@Autowired
CarService carService;


@RequestMapping("/cars")
public String getCars(Model model) {
    List<Car> CarList = carService.getAllCars();
    model.addAttribute("cars", CarList);
    return "cars";
}

@RequestMapping("/newcar")
public String newCar(Model model) {
    model.addAttribute("car", new Car());
    return "newcar";
}

// W TYM WYPADKU KOMUNIKACJA IDZIE OD KLIENTA, W atrybucie podajemy co otrzymujemy ( nmowe auto)
@RequestMapping(value = "/cars", method = RequestMethod.POST)
public String saveCar(Car car) {
    carService.saveCar(car);
    return "redirect:/cars";
}

//pobieranie parametru z adresu URL
@RequestMapping("/car")
public String getCar(@RequestParam("plate") String plate, Model model) {
    Car car = carService.getCar(plate);
    model.addAttribute("car", car);
    return "car";
}

@RequestMapping(value = "/car/delete/{plate}")
@Transactional
public String deleteCar(@PathVariable("plate") String plate, Model model) {
    carService.deleteCar(plate);
    return "redirect:/cars";
}
}
包com.RentCar.Rent_A_car.domain.Controllers;
导入com.RentCar.Rent_A_car.domain.car;
导入com.RentCar.Rent_A_car.domain.Repository.CarsRepository;
导入com.RentCar.Rent_A_car.domain.Services.CarService;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入javax.transaction.Transactional;
导入javax.websocket.server.PathParam;
导入java.util.ArrayList;
导入java.util.List;
@控制器
公共类汽车控制器{
//nie powinno siękomunikowaćmiędzy baządanych a kontrolerem,pomiędzy powinien byćserwis。
@自动连线
汽车服务;
@请求映射(“/cars”)
公共字符串getCars(模型){
List CarList=carService.getAllCars();
model.addAttribute(“汽车”,CarList);
归还“汽车”;
}
@请求映射(“/newcar”)
公共字符串新车(模型){
model.addAttribute(“car”,newcar());
返回“新车”;
}
//W TYM WYPADKU KOMUNIKACJA IDZIE OD KLIENTA,W atrybucie podajemy co otrzymujemy(nmowe auto)
@RequestMapping(value=“/cars”,method=RequestMethod.POST)
公共字符串存储车(Car-Car){
carService.saveCar(car);
返回“重定向:/cars”;
}
//波比拉尼酒店
@请求映射(“/car”)
公共字符串getCar(@RequestParam(“板”)字符串板,模型){
Car Car=carService.getCar(车牌);
model.addAttribute(“汽车”,汽车);
归还“汽车”;
}
@请求映射(value=“/car/delete/{plate}”)
@交易的
公共字符串deleteCar(@PathVariable(“板”)字符串板,型号){
carService.deleteCar(车牌);
返回“重定向:/cars”;
}
}

求你了,我找不到问题所在。例如,如果要删除,有什么问题?可能问题出在模板中?

问题出在这里:

public void DellateCar(String plate){
    em.remove(plate);
}
您必须传递托管汽车对象或对其的引用,而不是一些模糊的字符串

public void DellateCar(String plate){
    Car c = em.getReference(Car.class, plate); // or em.find(Car.class, plate);
    em.remove(c);
}

当您仅计划删除对象时,可以使用
getReference(entityclass,id)
,这样实际上就不会遇到从数据库加载对象的麻烦。否则使用
find(entityclass,id)
问题在这里:

public void DellateCar(String plate){
    em.remove(plate);
}
您必须传递托管汽车对象或对其的引用,而不是一些模糊的字符串

public void DellateCar(String plate){
    Car c = em.getReference(Car.class, plate); // or em.find(Car.class, plate);
    em.remove(c);
}

当您仅计划删除对象时,可以使用
getReference(entityclass,id)
,这样实际上就不会遇到从数据库加载对象的麻烦。否则,请使用
find(entityclass,id)

您得到的确切错误消息(包括堆栈跟踪)是什么?“我是入门级的”-因此,也许您不应该从“一个小web应用程序”开始,包括Spring和Hibernate等复杂框架?您得到的确切错误消息(包括堆栈跟踪)是什么?“我是入门级的”-所以也许你不应该从“一个小web应用”开始,包括像Spring和Hibernate这样的复杂框架?