---
title: "JPA非主键多对一关联"
author: "wing123"
date: "2013-08-23T14:50:29Z"
tags: []
categories: ["移动开发"]
slug: "jpa"
draft: false
lastmod: "2013-08-23T14:50:29Z"
id: 1929944
url: "https://wing123.iteye.com/blog/1929944"
---

SQL:

 

 

```
select * from A

inner join B where A.sn = B.cp_cn
```

 

 

 

 

一对多： A

 

 

```
private List<B> cpGbs;

@OneToMany(mappedBy = "a", fetch = FetchType.LAZY)
	public List<B> getCpGbs() {
        return cpGbs;
    }

    public void setCpGbs(List<B> cpGbs) {
        this.cpGbs = cpGbs;
    }
```

 

 

多对一：B

 

 

```
private A a;

@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CP_SN", referencedColumnName="sn",  insertable = false, updatable = false)
    public A getA() {
        return a;
    }
```

 name = "CP\_SN" -- 本表中的字段

 referencedColumnName="sn" -- 关联表的字段