Reputation: 4096
I am using spring boot and i want to cache some data here is my entity and repository classes
Places entity class:
@Entity
@Table(name = "PLACE_MASTER")
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Component
public class PlaceMaster extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PLACE_MASTER_ID")
@JsonView(View.Place.class)
private long placeMasterId;
@Column(name = "PLACE_NAME")
@JsonView(View.Place.class)
private String placeName;
@Column(name = "ALT_PLACE_NAME")
private String alternatePlaceName;
@Column(name = "PINCODE")
private String pinCode;
@Column(name="DISTRICT_NAME")
private String districtName;
@ManyToOne (cascade = CascadeType.ALL)
@JoinColumn(name = "STATE_MASTER_ID")
private StateMaster stateMaster;
}
state entity class:
@Entity
@Table(name = "STATE_MASTER")
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
//@ToString
public class StateMaster extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "STATE_MASTER_ID")
private long stateMasterId;
@Column(name = "STATE_NAME")
private String stateName;
@Column(name = "STATE_CODE")
private String stateCode;
@OneToMany(mappedBy = "stateMaster", cascade = CascadeType.ALL)
private List<PlaceMaster> placeMaster = new ArrayList<PlaceMaster>();
}
public interface PlaceMasterRepository extends JpaRepository<PlaceMaster, Long> {
@Override
@Cacheable("places")
public List<PlaceMaster> findAll();
}
bootstrap application class:
@SpringBootApplication
@EnableCaching
@EnableAsync
@EnableTransactionManagement
public class myApplication {
@Autowired
private PlaceMasterRepository placeMasterRepository;
public static void main(String[] args) throws Throwable {
SpringApplication.run(GoyaanaApplication.class, args);
}
}
when i run the the spring boot application i am getting the below error. Is it due to biderectional dependency. Please help
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference? at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
Upvotes: 0
Views: 829
Reputation: 15086
It is generally best to avoid circular dependencies between your bean, e.g. by moving @EnableCaching
, @EnableAsync
, @EnableTransactionManagement
to separate @Configuration class.
In cases where it is not possible, you can get around this by adding a @Lazy annotation on the @Autowired bean. This will create a lazy proxy that will get resolved at runtime. This comes with a caveat - you can't be sure at the start of the application if it is wired up correctly.
@Lazy
@Autowired
private PlaceMasterRepository placeMasterRepository;
For details see Spring documentation.
Upvotes: 1