|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import java.io.Serializable; |
| 9 | +import javax.persistence.Column; |
| 10 | +import javax.persistence.Entity; |
| 11 | +import javax.persistence.FetchType; |
| 12 | +import javax.persistence.Id; |
| 13 | +import javax.persistence.JoinColumn; |
| 14 | +import javax.persistence.ManyToOne; |
| 15 | +import javax.persistence.Table; |
| 16 | + |
| 17 | +import org.hibernate.cfg.Configuration; |
| 18 | + |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import io.vertx.ext.unit.TestContext; |
| 23 | + |
| 24 | +public class ManyToOneMergeTest extends BaseReactiveTest { |
| 25 | + |
| 26 | + @Override |
| 27 | + protected Configuration constructConfiguration() { |
| 28 | + Configuration configuration = super.constructConfiguration(); |
| 29 | + configuration.addAnnotatedClass( AcademicYearDetailsDBO.class ); |
| 30 | + configuration.addAnnotatedClass( CampusDBO.class ); |
| 31 | + return configuration; |
| 32 | + } |
| 33 | + |
| 34 | + @Before |
| 35 | + public void populateDb(TestContext context) { |
| 36 | + CampusDBO campusDBO2 = new CampusDBO(); |
| 37 | + campusDBO2.setId( 42 ); |
| 38 | + campusDBO2.setCampusName( "Kuchl" ); |
| 39 | + |
| 40 | + CampusDBO campusDBO = new CampusDBO(); |
| 41 | + campusDBO.setId( 66 ); |
| 42 | + campusDBO.setCampusName( "Qualunquelandia" ); |
| 43 | + |
| 44 | + AcademicYearDetailsDBO academicYearDetailsDBO = new AcademicYearDetailsDBO(); |
| 45 | + academicYearDetailsDBO.setId( 69 ); |
| 46 | + academicYearDetailsDBO.setCampusId( campusDBO ); |
| 47 | + academicYearDetailsDBO.setCreatedUsersId( 12 ); |
| 48 | + academicYearDetailsDBO.setRecordStatus( 'F' ); |
| 49 | + academicYearDetailsDBO.setModifiedUsersId( 66 ); |
| 50 | + test( context, |
| 51 | + getMutinySessionFactory().withTransaction( (session, transaction) -> session.persistAll( |
| 52 | + campusDBO, campusDBO2, |
| 53 | + academicYearDetailsDBO |
| 54 | + ) ) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void test(TestContext context) { |
| 60 | + test( context, getMutinySessionFactory() |
| 61 | + .withSession( session -> session |
| 62 | + .createQuery( "select dbo from AcademicYearDetailsDBO dbo", AcademicYearDetailsDBO.class ) |
| 63 | + .getSingleResult() ) |
| 64 | + .chain( dbo -> { |
| 65 | + dbo.setRecordStatus( 'A' ); |
| 66 | + System.out.println( dbo.getCampusId().getId() );//for example here campus id is 11 |
| 67 | + CampusDBO campusDBO = new CampusDBO(); |
| 68 | + campusDBO.setId( 5 ); |
| 69 | + dbo.setCampusId( campusDBO ); // need to update as 5 |
| 70 | + return getMutinySessionFactory() |
| 71 | + .withTransaction( (session, transaction) -> { |
| 72 | + dbo.setCampusId( session.getReference( CampusDBO.class, 42 ) ); |
| 73 | + return session.merge( dbo ); |
| 74 | + } ); |
| 75 | + } ) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @Entity(name = "AcademicYearDetailsDBO") |
| 80 | + @Table(name = "erp_academic_year_detail") |
| 81 | + static class AcademicYearDetailsDBO implements Serializable { |
| 82 | + @Id |
| 83 | + @Column(name = "erp_academic_year_detail_id") |
| 84 | + private Integer id; |
| 85 | + |
| 86 | + // LAZY will work |
| 87 | + @ManyToOne(fetch = FetchType.EAGER) |
| 88 | + @JoinColumn(name = "erp_campus_id") |
| 89 | + private CampusDBO campusId; |
| 90 | + |
| 91 | + @Column(name = "record_status") |
| 92 | + private char recordStatus; |
| 93 | + |
| 94 | + @Column(name = "created_users_id") |
| 95 | + private Integer createdUsersId; |
| 96 | + |
| 97 | + @Column(name = "modified_users_id") |
| 98 | + private Integer modifiedUsersId; |
| 99 | + |
| 100 | + public Integer getId() { |
| 101 | + return id; |
| 102 | + } |
| 103 | + |
| 104 | + public void setId(Integer id) { |
| 105 | + this.id = id; |
| 106 | + } |
| 107 | + |
| 108 | + public CampusDBO getCampusId() { |
| 109 | + return campusId; |
| 110 | + } |
| 111 | + |
| 112 | + public void setCampusId(CampusDBO campusId) { |
| 113 | + this.campusId = campusId; |
| 114 | + } |
| 115 | + |
| 116 | + public char getRecordStatus() { |
| 117 | + return recordStatus; |
| 118 | + } |
| 119 | + |
| 120 | + public void setRecordStatus(char recordStatus) { |
| 121 | + this.recordStatus = recordStatus; |
| 122 | + } |
| 123 | + |
| 124 | + public Integer getCreatedUsersId() { |
| 125 | + return createdUsersId; |
| 126 | + } |
| 127 | + |
| 128 | + public void setCreatedUsersId(Integer createdUsersId) { |
| 129 | + this.createdUsersId = createdUsersId; |
| 130 | + } |
| 131 | + |
| 132 | + public Integer getModifiedUsersId() { |
| 133 | + return modifiedUsersId; |
| 134 | + } |
| 135 | + |
| 136 | + public void setModifiedUsersId(Integer modifiedUsersId) { |
| 137 | + this.modifiedUsersId = modifiedUsersId; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Entity(name = "CampusDBO") |
| 142 | + @Table(name = "erp_campus") |
| 143 | + static class CampusDBO implements Serializable { |
| 144 | + @Id |
| 145 | + @Column(name = "erp_campus_id") |
| 146 | + private Integer id; |
| 147 | + |
| 148 | + @Column(name = "campus_name") |
| 149 | + private String campusName; |
| 150 | + |
| 151 | + public Integer getId() { |
| 152 | + return id; |
| 153 | + } |
| 154 | + |
| 155 | + public void setId(Integer id) { |
| 156 | + this.id = id; |
| 157 | + } |
| 158 | + |
| 159 | + public String getCampusName() { |
| 160 | + return campusName; |
| 161 | + } |
| 162 | + |
| 163 | + public void setCampusName(String campusName) { |
| 164 | + this.campusName = campusName; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments