JPA JoinColumns

Designates a ManyToOne or OneToOne relationship attribute that provides the mapping for an EmbeddedId primary key, an attribute within an EmbeddedId primary key, or a simple primary key of the parent entity. The value element specifies the attribute within a composite key to which the relationship attribute corresponds. If the entity's primary key is of the same Java type as the primary key of the entity referenced by the relationship, the value attribute is not specified.

@Getter

@Setter

public class BLCompanyId {

@Column(name = "CMP_ID", nullable = false, length = 20)

private String cmpId;

@Column(name = "CMP_CODE", nullable = false, length = 20)

private String cmpCode;

}

@Getter

@Setter

@Entity

@Table(name = "BLCOMPANY")

public class BLCompany {

@EmbeddedId

private BLCompanyId id;

private String cmpName;

@OneToMany(mappedBy = "cmp")

private List<BLEmployee> empList;

}

@Getter

@Setter

public class BLEmployeeId {

@Column(name = "CMP_ID", nullable = false, length = 20)

private String cmpId;

@Column(name = "CMP_CODE", nullable = false, length = 20)

private String cmpCode;

@Column(name = "EMP_ID", nullable = false, length = 20)

private String empCode;

}

@Getter

@Setter

@Entity

@Table(name = "BLEMPLOYEE")

public class BLEmployee {

@EmbeddedId

private BLEmployeeId id;

private String empAcc;

private String empName;

@Getter

@Setter

@Entity

@Table(name = "BLEMPLOYEE")

public class BLEmployee {

@EmbeddedId

private BLEmployeeId id;

private String empName;

//@MapsId

@ManyToOne(fetch = FetchType.LAZY, optional = false)

@JoinColumns({

@JoinColumn(name = "CMP_ID", referencedColumnName = "CMP_ID", nullable = false , insertable = false, updatable = false ),

@JoinColumn(name = "CMP_CODE", referencedColumnName = "CMP_CODE", nullable = false , insertable = false, updatable = false )})

// @JoinColumns({

// @JoinColumn(name = "CMP_ID", referencedColumnName = "CMP_ID", nullable = false/*, insertable = false, updatable = false*/),

// @JoinColumn(name = "CMP_CODE", referencedColumnName = "CMP_CODE", nullable = false/*, insertable = false, updatable = false*/)})

private BLCompany cmp;

}

@Repository

public interface BLCompanyRepository extends CrudRepository<BLCompany, BLCompanyId> {

}

@Repository

public interface BLEmployeeRepository extends CrudRepository<BLEmployee, BLEmployeeId> {

}

@Component

public class BLSearchRepository {

@Autowired

private EntityManager entityManager;

public List<BLEmployee> findEmpList(String companyId) {

CriteriaBuilder cb = entityManager.getCriteriaBuilder();

CriteriaQuery<BLEmployee> cq = cb.createQuery(BLEmployee.class);

Root<BLEmployee> emp = cq.from(BLEmployee.class);

Root<BLCompany> cmp = cq.from(BLCompany.class);

Predicate cmpIdPredicate = cb.equal(emp.get("id").get("cmpId"), cmp.get("id").get("cmpId"));

Predicate empPredicate = cb.equal(emp.get("id").get("cmpId"),companyId);

cq.select(emp);

Predicate condition = cb.and(cmpIdPredicate, empPredicate);

cq.where(condition);

List<BLEmployee> results = entityManager.createQuery(cq).getResultList();

return results;

}

}

//@TestPropertySource("classpath:application.properties")

@EnableJpaRepositories({"idmap.repo"})

@ComponentScan({"idmap.repo"})

@EntityScan({"idmap.entity"})

@Configuration

class BLRepositoryCfg {

}

@Transactional(propagation = Propagation.SUPPORTS)

@ContextConfiguration(classes = BLRepositoryCfg.class)

@DataJpaTest

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

public class BLSearchRepositoryTest {

@Autowired

BLSearchRepository rep;

@Autowired

BLCompanyRepository cmpRep;

@Autowired

BLEmployeeRepository empRep;

@Test

void test() {

createCmp();

createEmp();

List<BLEmployee> list=rep.findEmpList("A0001");

assertEquals(true, list.size()>0);

}

void createCmp() {

createCmp("A0001","CD0001","lemon");

createCmp("A0002","CD0002","apple");

}

void createCmp(String cmpId, String cmpCode, String cmpName) {

BLCompanyId id=new BLCompanyId();

id.setCmpCode(cmpCode);

id.setCmpId(cmpId);

BLCompany cmp=new BLCompany();

cmp.setCmpName(cmpName);

cmp.setId(id);

cmpRep.save(cmp);

}

void createEmp() {

createEmp("A0001","E10001","E1One","CD0001");

createEmp("A0001","E10002","E1Two","CD0001");

createEmp("A0002","E20001","E2One","CD0002");

createEmp("A0002","E20002","E2Two","CD0002");

}

void createEmp(String cmpId, String empCode, String empName, String cmpCode) {

BLEmployeeId id=new BLEmployeeId();

id.setCmpId(cmpId);

id.setCmpCode(cmpCode);

id.setEmpCode(empCode);

BLEmployee e=new BLEmployee();

e.setId(id);

e.setEmpName(empName);

empRep.save(e);

}

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project

xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>3.1.2</version>

<relativePath/>

<!-- lookup parent from repository -->

</parent>

<groupId>com.example</groupId>

<artifactId>idclasstest</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>idclasstest</name>

<description>Demo project for Spring Boot</description>

<properties>

<java.version>17</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

<dependency>

<groupId>com.mysql</groupId>

<artifactId>mysql-connector-j</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

application.properties

spring.jpa.generate-ddl=false

spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.format_sql=true

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL8Dialect

spring.jpa.properties.hibernate.default_schema=

spring.datasource.url=jdbc:mysql://localhost:3306/luck?serverTimezone=UTC

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

被MapsId和JoinColumns搞得很无语,只能写个好用的例子出来玩了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值