我正在使用Spring Data JPA从数据库中检索数据。
我有以下两个表:**部门和班级。**部门和班级之间存在一对多关系。
Department columns: Dept_Id, Dept_Name, CreatedDate
Class columns: Class_Id, Dept_Id, Class_Type, Class_Name
字符串
我正在尝试根据班级类型和班级名称获取部门列表。
我创建了如下实体:
@Table(name = "Department")
@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Dept_Id", nullable = false)
private Long DeptId;
@Column(name = "Dept_Name", nullable = false, length = 256, unique = true)
private String DeptName;
@Column(name = "CreatedDate", length = 256)
private String CreatedDate;
@OneToMany(mappedBy = "Department", fetch = FetchType.EAGER)
@ToString.Exclude
private Set<Class> classess;
}
@Table(name = "Class")
@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Class {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Class_Id", nullable = false)
private Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "Dept_Id", nullable = false)
@ToString.Exclude
@JsonIgnore
private Department department;
@Column(name = "Class_Type", length = 256)
private String classType;
@Column(name = "Class_Name", length = 256)
private String className;
}
型
部门资料库
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
@Query(nativeQuery = true,
value="SELECT dept.* " +
"FROM Department dept INNER JOIN Class cls ON dept.DeptId = cls.DeptId " +
"WHERE cls.classType = 'class1' " +
"AND cls.className IN :name " +
"ORDER BY START_TIME DESC")
List<Department> findDepartmentByClassType(@Param("name") List<String> name);
}
型
ClassRepository
@Repository
public interface ClassRepository extends JpaRepository<Class, Long> {
}
型
售后服务:
@Service
public class DepartmentService {
public List<Department> getDepartmentsForClassType(List<String> className) {
departmentRepository.findDepartmentByClassType(className);
}
型
当我运行应用程序时,我得到以下错误:
java.sql.SQLRecoverableException: No more data to read from socket
at oracle.jdbc.driver.T4CMAREngineNIO.prepareForUnmarshall(T4CMAREngineNIO.java:784) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CMAREngineNIO.unmarshalUB1(T4CMAREngineNIO.java:429) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:407) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:268) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:501) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:1292) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:1025) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:743) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:793) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:57) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:747) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:562) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar:na]
j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
型
我相信这个错误是由于我Map两个实体的方式中的代码问题造成的。我这么说的原因是因为如果我更新仓库的select语句,只从department表获取记录,而不与class表连接,那么应用程序就可以成功运行。有人能帮助我弄清楚我在这里做错了什么吗?
2条答案
按热度按时间rkue9o1l1#
如果连接到类表导致问题,请检查以下内容:
1.确保类表中存在匹配的记录。
1.验证连接条件的准确性。
1.检查类表的访问权限。
1.检查实体字段和表列之间的Map。
emeijp432#
我能够解决这个问题。我必须在Department实体中添加@OneToMany关系,如下所示
字符串
另外,将
@Transactional("transactionManager")
添加到服务中。