Java 针对未映射类使用@OneToMany或@ManyToMany(多数据库连接)
我有一个JPA Spring项目。我添加了两个数据库配置文件。当我想运行project时,是否收到此错误? 我正在使用javax.persistence.Entity 用户的实体Java 针对未映射类使用@OneToMany或@ManyToMany(多数据库连接),java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我有一个JPA Spring项目。我添加了两个数据库配置文件。当我想运行project时,是否收到此错误? 我正在使用javax.persistence.Entity 用户的实体 package com.example.local.model.entity; @Entity @Table( name = "tbl_user", uniqueConstraints = { @UniqueConstraint(name = "uc_user_data_username"
package com.example.local.model.entity;
@Entity
@Table(
name = "tbl_user",
uniqueConstraints = {
@UniqueConstraint(name = "uc_user_data_username", columnNames = "username"),
@UniqueConstraint(name = "uc_user_data_email", columnNames = "email")
})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@ToString(exclude = "password")
public class User extends Auditing implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_user_data")
@SequenceGenerator(sequenceName = "seq_user_data", allocationSize = 1, name = "seq_user_data")
private Long id;
@NotNull
// @Pattern(regexp = ApplicationConstants.Entity.MOBILE_REGEX)
@NationalCode
// @Size(min = 11, max = 11)
@Column(name = "username", length = 11, nullable = false)
private String username;
@Pattern(regexp = regex.MOBILE_REGEX)
@Column(name = "mobile", length = 11)
private String mobile;
@Email
@Size(min = 7, max = 50)
@Column(name = "email", length = 50)
private String email;
@JsonIgnore
@NotNull
// @Size(min = 5, max = 50)
@Column(name = "password", length = 60, nullable = false)
private String password;
@Size(max = 256)
@Column(name = "image", length = 256)
private String image;
@NotNull
@Size(max = 30)
@Column(name = "first_name", length = 30)
private String firstName;
@NotNull
@Size(max = 30)
@Column(name = "last_name", length = 30)
private String lastName;
@Column(name = "national_code", length = 11)
private String nationalCode;
@Size(max = 20)
@Column(name = "activation_key", length = 20)
@JsonIgnore
private String activationKey;
@Size(max = 20)
@Column(name = "reset_key", length = 20)
@JsonIgnore
private String resetKey;
@Column(name = "reset_at")
private LocalDateTime resetAt = null;
@ColumnDefault("1")
@Column(name = "account_enabled")
private Boolean isAccountEnabled = true;
@ColumnDefault("0")
@Column(name = "account_expired")
private Boolean isAccountExpired = false;
@ColumnDefault("0")
@Column(name = "credentials_expired")
private Boolean isCredentialsExpired = false;
@ColumnDefault("0")
@Column(name = "account_locked")
private Boolean isAccountLocked = false;
@ColumnDefault("1")
@Column(name = "priority")
private Byte priority = 1;
@ColumnDefault("1")
@Column(name = "is_active")
private Boolean isActive = true;
@Column(
name = "user_type",
length = 10,
columnDefinition = "VARCHAR2(10 CHAR) DEFAULT 'USER'",
insertable = false)
@Enumerated(value = EnumType.STRING)
private UserType userType = UserType.USER;
@JsonIgnore
@ManyToOne
@JsonIgnoreProperties("")
private AgencyType agencyType;
@JsonIgnore
@ManyToMany
@JoinTable(
name = "tbl_auth_user_authority",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size = 20)
private Set<Authority> authorities = new HashSet<>();
@JsonIgnore
@ManyToMany
@JoinTable(
name = "tbl_auth_user_permission",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "permission_name", referencedColumnName = "name")})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size = 20)
private Set<Permission> permissions = new HashSet<>();
@JsonIgnore
@ManyToMany
@JoinTable(
name = "tbl_auth_user_permission_omitted",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "permission_name", referencedColumnName = "name")})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size = 20)
private Set<Permission> omittedPermissions = new HashSet<>();
}
数据库配置文件
package com.example.local.config;
@Configuration
@PropertySource({"classpath:application.yml"})
@EnableJpaRepositories(
basePackages = "com.sepehrpay.mps.model.dao")
@EnableJpaAuditing(auditorAwareRef = "auditorProvider") // "springSecurityAuditorAware"
public class LocalDbConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("database.platform", "org.hibernate.dialect.Oracle10gDialect");
return builder
.dataSource(dataSource)
.packages("com.sepehrpay.mps.model.entity", "com.sepehrpay.mps.model.mapper")
.persistenceUnit("MpsPU")
.properties(properties)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
AuditorAware<Long> auditorProvider() {
return new AuditorProviderAware();
}
}
package com.example.local.config;
@配置
@PropertySource({“classpath:application.yml})
@授权代理(
basePackages=“com.seperpay.mps.model.dao”)
@启用JPA审核(auditorAwareRef=“auditorProvider”)/“springSecurityAuditorAware”
公共类LocalDbConfiguration{
@初级的
@Bean(name=“dataSource”)
@ConfigurationProperties(前缀=“spring.datasource”)
公共数据源userDataSource(){
返回DataSourceBuilder.create().build();
}
@初级的
@Bean(name=“entityManagerFactory”)
公共LocalContainerEntityManagerFactoryBean
实体管理工厂(
EntityManager工厂生成器生成器,
@限定符(“数据源”)数据源数据源
) {
映射属性=新的HashMap();
properties.put(“hibernate.hbm2ddl.auto”、“update”);
properties.put(“database.platform”、“org.hibernate.dialogue.oracle10gDialogue”);
返回生成器
.dataSource(数据源)
.packages(“com.seperpay.mps.model.entity”、“com.seperpay.mps.model.mapper”)
.持久性单位(“MpsPU”)
.物业(物业)
.build();
}
@初级的
@Bean(name=“transactionManager”)
公共平台事务管理器事务管理器(
@限定符(“entityManagerFactory”)entityManagerFactory
实体管理工厂
) {
返回新的JpaTransactionManager(entityManagerFactory);
}
@豆子
AuditorAware auditorProvider(){
返回新的AuditorProviderware();
}
}
错误是:
org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany
以未映射的类为目标:
com.example.local.model.entity.User.permissions[com.example.local.core.auth.domain.Permission]
我已经通过在entityManagerFactory中更改扫描包地址解决了我的问题我已经通过在entityManagerFactory中更改扫描包地址解决了我的问题我希望您正在使用javax.persistence.Entity for@Entity,或者您可能忘记在我正在使用的hibernate配置文件类元素中注册它javax.persistence.Entity您能解释一下在hibernate配置文件类中注册吗???您的hibernate-Entity映射类在哪里?我在LocalContainerEntityManagerFactoryBean的包中添加了一些路径,之后问题解决了,但我有一个新的错误。“无法创建唯一密钥约束”我希望您使用的是javax.persistence.Entity for@Entity,或者您可能忘记了在hibernate配置文件类element中注册它。我使用的是javax.persistence.Entity,您能解释一下在hibernate配置文件类中注册吗???您的hibernate-Entity映射类在哪里?我在LocalContainerEntityManagerFactoryBean,然后问题解决了,但我有了新的错误。“无法创建唯一密钥约束”
package com.example.local.config;
@Configuration
@PropertySource({"classpath:application.yml"})
@EnableJpaRepositories(
basePackages = "com.sepehrpay.mps.model.dao")
@EnableJpaAuditing(auditorAwareRef = "auditorProvider") // "springSecurityAuditorAware"
public class LocalDbConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("database.platform", "org.hibernate.dialect.Oracle10gDialect");
return builder
.dataSource(dataSource)
.packages("com.sepehrpay.mps.model.entity", "com.sepehrpay.mps.model.mapper")
.persistenceUnit("MpsPU")
.properties(properties)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
AuditorAware<Long> auditorProvider() {
return new AuditorProviderAware();
}
}