TL; DR:帮助我按两列连接一个表,一列通过固定值,另一列通过变量值。是@OneToOne。
执行此操作,但要使其处于休眠状态:
SELECT t.*, c.*
FROM grid.app_users t
JOIN app_dictionaries c on c.primary = 'USER_STATES' and c.secondary = t.state
;
我试图将另一个连接列添加到我的用户实体(映射到表app_users
)。
此列必须由primary
(固定为 USER_STATES )和secondary
(可变,在这种情况下为 A 或 P {em>)代码放在app_dictionaries
表上。
帮我找到合适的加入方法。
我本可以创建一个 视图 并将其与上述专栏一并加入,但这会导致丧失insert
和update
的能力在app_users
上。
我对Hibernate和Spring JPA存储库不是很友好。
我的用户实体:
package com.core.app.admin.entity;
import com.core.app.admin.auth.ValidPassword;
import com.core.app.admin.repository.RState;
import com.core.app.dicts.entity.EDict;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "app_users")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Data
public class EUser {
@Autowired
RState repository;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private int userId;
@Column(name = "user_group_id")
private int userGroupId;
@Column(name = "name")
private String name;
@Column(name = "login")
private String login;
@ValidPassword
@Column(name = "password")
private String password;
@Column(name = "state")
private String state;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(
name = "app_user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<ERole> roles;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "app_dictionaries")
private EDict stateName = repository.getByPrimaryAndSecondary();
}
我的字典实体:
package com.core.app.dicts.entity;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(
name = "app_dictionaries",
uniqueConstraints = {@UniqueConstraint(columnNames = {"primary", "secondary"})}
)
@Data
public class EDict implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "line_id")
private int lineId;
@Column(name = "primary")
private String primary;
@Column(name = "secondary")
private String secondary;
@Column(name = "name")
private String name;
@Column(name = "state")
private String state;
}
我的状态/词典存储库:
package com.core.app.admin.repository;
import com.core.app.dicts.entity.EDict;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface RState extends JpaRepository<EDict, Integer> {
List<EDict> findByPrimary(String primary);
EDict getByPrimaryAndSecondary();
}
我的错误:
[org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#51933ecb': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.core.app.admin.entity.EUser.stateName, referenced property unknown: com.core.app.dicts.entity.EDict.app_dictionaries
谢谢。