暑期项目实训SaaS云服务系统开发—云会议系统——7.4实体类、Repository接口以及部分Service接口的编写

1、主要开发内容

​ 修改了部分依赖,构造资源管理与业务发放的相关Entity实体类、Repository接口,以及部分的Service接口,对于部分Repository接口进行测试。

​ 部分代码展示:

User类

/**
 * @author: Joheey
 * Date: 2021/7/1
 * Time: 20:00
 * Description:用户实体
 */
@Data
@Entity
@Table(name = "user")
public class User {

    @Id
    @Column(name = "id",unique = true,nullable = false,length = 11)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "username",nullable = false)
    private String name;

    @Column(name = "password",nullable = false)
    private String password;

    @Column(name = "permission",nullable = false)
    private Integer permission;

    @Column(name = "telephone")
    private Integer telephone;

    @Column(name = "sex")
    private Integer sex;

    @Column(name = "deleted",nullable = false)
    private Boolean deleted;

    @Column(name = "create_time")
    @CreatedDate
    private Timestamp create_time;

    @Column(name = "modify_time")
    @LastModifiedDate
    private Timestamp modify_time;

    @ManyToOne(fetch = FetchType.EAGER)//强制加载
    @JoinColumn(name = "company_id")
    private Company company;

    @ManyToOne(fetch = FetchType.EAGER)//强制加载
    @JoinColumn(name = "department_id")
    private Department department;

    public User(String username, String password, Integer permission) {
        this.name = username;
        this.password = password;
        this.permission = permission;
    }

    public User() {

    }

    public String getCreateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return dateFormat.format(create_time);
    }

    public String getModifyTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return dateFormat.format(modify_time);
    }
}

用户Repository接口:

public interface UserRepository extends JpaRepository<User, Integer> {

    User findUserById(Integer userId);

    User findByName(String name);

    User findByTelephone(Integer telephone);

    List<User> findByPermission(Integer permission);

    List<User> findByCompanyId(Integer companyId);

    List<User> findByDepartmentId(Integer departmentId);

}

2、开发中遇到的问题
2.1、org.springframework.beans.factory.BeanCreationException
	org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource 

    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    
    Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table company_departments add constraint FKs4y3by8y73ej67cvn9leliuq1 foreign key (departments_id) references department (id)]
	
    Caused by: java.sql.SQLException: Referencing column 'departments_id' and referenced column 'id' in foreign key constraint 'FKs4y3by8y73ej67cvn9leliuq1' are incompatible.                                                                                                                      

​ 根据报错信息可以大致了解到,由于没有添加mappedBy属性,JPA自动为我们创建了关于Department与Company的关联表,而在其中department_id与id产生了外键冲突。初始的一对多映射如下:

//企业员工(一对多关系)
@OneToMany(fetch = FetchType.LAZY)
private Set<User> users;

解决方法

​ 在company类中,为@OneToMany添加mappedBy字段,问题解决

@OneToMany(mappedBy = "company",fetch = FetchType.LAZY)
private Set<User> users;

@OneToMany(mappedBy = "company",fetch = FetchType.LAZY)
private Set<Department> departments;

单元测试

​ 单元测试的过程中,发现了部分问题

1、对于User类与Company类,在输出类对象的时候会出现循环调用的问题。

解决方案:重写many方的toString方法

@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", password='" + password + '\'' +
            ", permission=" + permission +
            ", telephone=" + telephone +
            ", sex=" + sex +
            ", is_logout=" + is_logout +
            ", create_time=" + create_time +
            ", modify_time=" + modify_time +
            '}';
}

2、当在repository接口中设置了延时加载时,出现如下报错:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.rjxy.Entity.Company.users, could not initialize proxy - no Session

由于设置了延时加载,通过get方法获取信息时,session已经关闭了,所以不能获取到对应信息。

解决方法:如果是在测试类中,在测试方法上添加一个@Transactional

通过上述方法后解决了这个问题,但是遇到了新的报错java.lang.StackOverflowError,根据错误内容中的SQL语句显示,很容易就看出来,在查询的过程中懒加载并没有起作用,company,department,user三个实体在查询的过程中不断相互调用,导致栈溢出。核心的问题还是懒加载失效。

以下是一种可能起效的方法:

https://blog.csdn.net/R_o_b_o_t_/article/details/115400514?spm=1001.2014.3001.5501

明天考试,具体问题的研究等到考完试再来解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值