Hibernate一对一注释表现得非常奇怪

时间:2018-05-05 21:20:22

标签: java spring hibernate jpa

我正在进行一对一的连接以使用注释获取结果。我有两个类,第一个是T_ARM_Details,第二个类是ARM_Ticket_Details和T_ARM_Alert_Type。我使用注释进行一对一连接,但连接没有发生,并从单个表中获取数据

package com.cts.met.bo;

import java.io.Serializable; 
import javax.persistence.CascadeType;
import javax.persistence.Column; 
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="T_ARM_Details")

public class ARM_Ticket_Details implements Serializable {   

@Id
@Column(name="ticket_id")

  String Ticket_id;

@Column(name="arm_alert_type_id")
int arm_alert_type_id;
 /*@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
 @JoinTable(name="T_arm_alert_type",joinColumns=@JoinColumn(name="arm_alert_type_id",referencedColumnName="arm_alert_type_id"),inverseJoinColumns=@JoinColumn(name= "response_status_id"))*/

 public int getArm_alert_type_id() {
return arm_alert_type_id;
  }

public void setArm_alert_type_id(int arm_alert_type_id) {
this.arm_alert_type_id = arm_alert_type_id;
}

private T_ARM_Alert_Type t_arm_alert_type;



 //private T_SLA_Status t_sla_statuse;

public String getTicket_id() {
  return Ticket_id;
 }

public void setTicket_id(String ticket_id) {
Ticket_id = ticket_id;
} 
 @OneToOne(fetch=FetchType.LAZY,mappedBy="ARM_Ticket_Details",
 cascade=CascadeType.ALL)
  @JoinTable(name="T_arm_alert_type", 
 joinColumns=@JoinColumn(name="arm_alert_type_id"))//,
 referencedColumnName="arm_alert_type_id"))
  //@JoinColumn(name="arm_alert_type_id")
 public T_ARM_Alert_Type getT_arm_alert_type() {
return t_arm_alert_type;
}

public void setT_arm_alert_type(T_ARM_Alert_Type t_arm_alert_type) {
this.t_arm_alert_type = t_arm_alert_type;
}


/*@ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
  @JoinTable(name="T_sla_status",
   joinColumns=@JoinColumn(name="resolution_status_id")//,
 //inverseJoinColumns=@JoinColumn(name= "response_status_id")
 )
 //@JoinColumn(name="status_id")
*/
/*public T_SLA_Status getT_sla_statuse() {
return t_sla_statuse;
  }

public void setT_sla_statuse(T_SLA_Status t_sla_statuse) {
this.t_sla_statuse = t_sla_statuse;
 }*/

}

下一个如下:

package com.cts.met.bo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

 @SuppressWarnings("serial")
 @Entity
  @Table(name="T_arm_alert_type")
  public class T_ARM_Alert_Type implements Serializable{
  @Id
 //@OneToOne(targetEntity=ARM_Ticket_Details.class,cascade=CascadeType.ALL)

//@JoinColumn(name="arm_alert_type_id",referencedColumnName="arm_alert_type_ 
  id")
  /*@OneToOne(fetch=FetchType.LAZY)
  @PrimaryKeyJoinColumn*/
    //@JoinColumn(name="arm_alert_type_id")
   int arm_alert_type_id;

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

@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ticket_id")
private ARM_Ticket_Details arm;
public ARM_Ticket_Details getArm() {
    return arm;
}
public void setArm(ARM_Ticket_Details arm) {
    this.arm = arm;
}
public int getArm_alert_type_id() {
    return arm_alert_type_id;
}
public void setArm_alert_type_id(int arm_alert_type_id) {
    this.arm_alert_type_id = arm_alert_type_id;
}
public String getArm_alert_desc() {
    return arm_alert_desc;
}
public void setArm_alert_desc(String arm_alert_desc) {
    this.arm_alert_desc = arm_alert_desc;
}

}

我的控制器看起来像这样:

 @RequestMapping(value = "/editTicket", method = RequestMethod.GET)
public ModelAndView editTicket(HttpServletRequest request){
    String TicketId = request.getParameter("id");
    ARM_Ticket_Details ticket = pmportalService.getARMTicketById(TicketId);
    ModelAndView model = new ModelAndView("editTicket");
    model.addObject("ticket", ticket);

    return model;
}

我希望在这里发生加入a,但是它给出了以下错误:

   select
    arm_ticket0_.ticket_id as ticket_i1_1_0_,
    arm_ticket0_.arm_alert_type_id as arm_aler2_1_0_,
    arm_ticket0_.t_arm_alert_type as t_arm_al3_1_0_ 
    from
    T_ARM_Details arm_ticket0_ 
    where
    arm_ticket0_.ticket_id=?

有人可以帮忙吗?顺便说一句,我是新手来的。

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您正在使用Lazy FetchType,您应该使用EAGER,或者明确跳过明确定义,因为它是默认的FetchType值。延迟加载用于出于性能原因,最常见的情况是当您具有OneToMany关系并加载太多对象时,如果它们不是真正需要,则会影响每个操作的性能。您很少需要对OneToOne关系使用延迟提取,因为您只使用一个额外的对象,这不会影响应用程序的性能,除非在特殊情况下可能存在大量对象。您可以在documentation中了解有关不同获取类型及其效果的更多信息。