spring 如何为一个有三个键(两个外部键和一个内部键)的实体正确地设计一个控制器和一个jsp页面?

68bkxrlz  于 2023-02-15  发布在  Spring
关注(0)|答案(1)|浏览(427)

我正在尝试制作一个Spring MVC应用程序。我有4个实体(Company,Pass_in_trip,Passenger,Trip)Pass_in_trip有3个键,分别是Passenger,Trip和Timestamp,我不知道如何正确地发布一个键,如何通过jsp页面将其传递到控制器,以及如何发布控制器本身,有谁能告诉我吗?还有一个有趣的问题是如何向数据库发出使用三个键搜索记录的请求。
谢谢
这是我现在能够写的,看看是否有任何错误

@Entity
@Table(name="company")
public class Company implements Serializable {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name="id_comp") 
private int id_comp;
    
@Column(name="name")
private String name;
//Getters and Setters
@Entity
@Table (name="pass_in_trip")
public class Pass_in_trip implements Serializable  {

@EmbeddedId
private KeysPass_in_trip key=new KeysPass_in_trip();

@Column(name="place")   
private String place;
//Getters and Setters
@Embeddable
public class KeysPass_in_trip implements Serializable{

@NotNull
@JoinColumn(name="date")
private Timestamp date=new Timestamp(System.currentTimeMillis());
    
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id_psg")
private Passenger  id_psg=new Passenger();

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "trip_no" )
private Trip trip_no=new Trip();

//Getters and Setters
//@Override hashCode and equals
@Entity
@Table(name="passenger")
public class Passenger  implements Serializable {

@Column(name="name")  
private String name;

@NotNull
@Id
@Column(name="id_psg")
@GeneratedValue(strategy = IDENTITY)
private int id_psg; 
//Getters and Setters
@Entity
@Table(name="trip")
public class Trip implements Serializable  {
        @NotNull
    @Id
    @Column(name="trip_no")
        private int trip_no;

        @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "id_comp")
    private Company comp=new Company();

    @Column(name="plane")
    private String plane;

        @Column(name="town_from")
    private String town_from;

        @Column(name="town_to")
    private String town_to;

        @Column(name="time_out")
    private Timestamp time_out;
        @Column(name="time_in")
    private Timestamp time_in;
//Getters and Setters
Conroller 
@Controller
@RequestMapping("/pass_in_trip/")
public class Aero_Controller_Pass_in_trip {
@Autowired  
private Aero_DAO service;

public void setService(Aero_DAO service) {
this.service = service;
    }
 @RequestMapping(method=RequestMethod.GET)
 public String list(Model uiModel) {
   List <Pass_in_trip> pass_in_trip=service.findallPass_in_trip();
   uiModel.addAttribute("pass_in_trip",pass_in_trip);
   return "/pass_in_trip/list";
         }
 @PreAuthorize("hasRole('ROLE_Admin')")
 @RequestMapping(value="delete/{id}",method=RequestMethod.GET)
 public String delete(@PathVariable("id")int id, Model uiModel) {
         if(service.findByIdPass_in_Trip(id)!=null)
             service.delete_Pass_in_trip(id);
         return "redirect:/pass_in_trip/";
     }
 @PreAuthorize("hasRole('ROLE_Admin')")
 @RequestMapping(value="update/{id}",method=RequestMethod.GET)
 public String updateform(@PathVariable("id")int id, Model uiModel) {
         System.out.println("upform");
          uiModel.addAttribute("pass_in_trip",service.findByIdPass_in_Trip(id));
          System.out.println("upform2");
          return "/pass_in_trip/edit";
      }
 @RequestMapping(value="update/0",method=RequestMethod.GET)
 public String newform(Model uiModel) {
         System.out.println("Привет!");
         return "/pass_in_trip/edit";
     }
@PreAuthorize("hasRole('ROLE_Admin')")
@RequestMapping(value="update/{id}",method = RequestMethod.POST)
public String update(Pass_in_trip pass_in_trip,BindingResult bindingResult,Model     uiModel,HttpServletRequest httprervletrequest , RedirectAttributes redirectatributes) {
        if (bindingResult.hasErrors()) {
    uiModel.addAttribute("pass_in_trip", pass_in_trip);
        return "pass_in_trip/update";}
        service.save(pass_in_trip);
        return "redirect:/pass_in_trip/";
    }
}
List.jsp
interested in this part:
    
            
<s:authorize access="hasRole('ROLE_Admin')">
<td><a href="update/${pass_in_trip.???(what should be here?)}"> To change </a></td>
<td><a href="delete/${pass_in_trip.???(what should be here?)}"> Delete </a></td>
                    </s:authorize>
tzdcorbm

tzdcorbm1#

看起来您有2个实体(乘客、行程)和一种桥表来定义多对多关系。

相关问题