JPA:获取单个记录时的多个select语句@OneToOne和@PrimaryKeyJoinColumn

时间:2013-01-02 11:41:29

标签: hibernate jpa

将我的实体视为具有共享主键的一对一关系的CPU和PROCESSOR。 一个CPU有一个处理器。

CPU.java

@Entity
@Table(name = "PKJoin_CPUt")
public class CPU {


    private int id;
    private String name;
    private Processor processor;

    @OneToOne(cascade = CascadeType.PERSIST, mappedBy = "cpu", optional = true,fetch=FetchType.LAZY)
    public Processor getProcessor() {
        return processor;
    }
    public void setProcessor(Processor processor) {
        this.processor = processor;
        if (null != processor) {
            processor.setCpu(this);
        }
    }
    private Processor2 processor2;

    @OneToOne(cascade = CascadeType.PERSIST, mappedBy = "cpu", optional = true,fetch=FetchType.LAZY)
    public Processor2 getProcessor2() {
        return processor2;
    }

    public void setProcessor2(Processor2 processor2) {
        this.processor2 = processor2;
        if (null != processor2) {
            processor2.setCpu(this);
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
        if (null != processor) {
            processor.setId(id);
        }
        if (null != processor2) {
            processor2.setId(id);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String deptName) {
        this.name = deptName;
    }

}

Processor.java

@Entity
@Table(name = "PKJoin_Processor")
public class Processor {

    private int id;
    private String name;

    private CPU cpu;

    @OneToOne(fetch=FetchType.LAZY)
    @PrimaryKeyJoinColumn
    public CPU getCpu() {
        return cpu;
    }

    public void setCpu(CPU cpu) {
        this.cpu = cpu;
    }
    @Id
    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;
    }
}

Main.java

public class Main {
  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("Hello");
  static EntityManager em = emf.createEntityManager();

  public static void main(String[] a) throws Exception {
    em.getTransaction().begin();

    new Main().findData();
   // new Main().persistData();

    em.getTransaction().commit();
    em.close();
    emf.close();    

  }

 public void findData(){      

    CPU cpu = em.find(CPU.class, 449);
    System.out.println(cpu.getProcessor().getName());   


  }
}

这样执行::

    Hibernate: select cpu0_.id as id11_0_, cpu0_.name as name11_0_ from PKJoin_CPUt cpu0_ where cpu0_.id=?
    Hibernate: select processor0_.id as id38_0_, processor0_.name as name38_0_ from  PKJoin_Processor processor0_ where processor0_.id=?
    Hibernate: select processor2x0_.id as id19_0_, processor2x0_.name as name19_0_ from PKJoin_Processor222 processor2x0_ where processor2x0_.id=?

当我只获取CPUName属性时,我不希望这三个查询被执行。我也不想加入。 我想只有第一个查询才能在我尝试获取CPU属性时执行。 需要解决方案吗?

2 个答案:

答案 0 :(得分:1)

问题在于,由于associatio是可选的,所以只要加载CPU,Hibernate就无法知道它是否有相关的处理器。因此,为了将处理器字段设置为null,或者将其设置为Processor的实例,如果存在具有CPU主键的处理器,则必须进行chck。

为避免这种情况,最简单的方法是使用指向处理器的CPU中的连接列映射关联。这样,如果join列为null,则Hibernate知道没有关联的CPU。如果它不为null,则可以将procssor字段设置为惰性代理。

AFAIK的另一种方式是使用@LazyToOne(NO_PROXY)instrument the bytecode。但我过去曾经遇到过这种情况的糟糕经历。也许它有所改善。

答案 1 :(得分:0)

在你的findData()中,你做cpu.getProcessor()。getName(),所以你得到第一个处理器的查询是合乎逻辑的。

你在CPU.setId()函数中做了很奇怪的事情,也许这就是加载处理器的原因。

你不应该这样做,你应该让hibernate来处理这个问题。对于基于主键的双向一对一关系,我认为您需要以下内容:

public class CPU { 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;


    @OneToOne(cascade = CascadeType.ALL, mappedBy = "cpu", fetch=FetchType.LAZY)
    private Processor processor; 

    ...
}

public class Processor {  
    @Id @OneToOne 
    @JoinColumn("id")
    private CPU cpu;  //combine @Id and @OneToOne, no int id needed.

    protected Processor(){}    

    public Processor(CPU cpu){
       this.cpu = cpu;
       cpu.setProcessor(this);
    }    
    ...
}

现在,如果您使用CPU保存处理器,其ID将神奇地填入CPU的ID。 确保以正确的顺序保存它们:

CPU cpu = new CPU();
em.save(cpu);
Processor p = new Processor(cpu);
em.save(p);