[英]Cannot Override onBeforeConvert: “…have the same erasure, yet neither overrides the other”
I've been trying but failing to imitate a Baeldung article 's walkthrough on how to trigger a save on a "child": an entity that is "contained" in another via Spring Boot's @DBRef annotation. 我一直在尝试但未能模仿Baeldung文章关于如何触发“子”的保存的演练:一个通过Spring Boot的@DBRef注释“包含”在另一个中的实体。 In my case, I'm trying to DBRef an Address (child) from a Customer. 就我而言,我正在尝试DBRef来自客户的地址(孩子)。
I'm posting my brief configuration file as well as the suspect code because I don't know if it is malformed. 我发布了我的简要配置文件以及可疑代码,因为我不知道它是否格式错误。
The code that I suspect is erroneous is in a class called "CustomerCascadeSaveMongoEventListener.java" under src > main > java > foo.bar, and looks like: 我怀疑错误的代码是在src> main> java> foo.bar下的一个名为“CustomerCascadeSaveMongoEventListener.java”的类中,看起来像:
@Override
public class CustomerCascadeSaveMongoEventListener extends AbstractMongoEventListener {
@Autowired
private MongoOperations mongoOperations;
@Override
public void onBeforeConvert(BeforeConvertEvent<Object> event) {
Object source = event.getSource();
if ((source instanceof Customer) && (((Customer) source).getAddress() != null)) {
mongoOperations.save(((Customer) source).getAddress());
}
}
}
IntelliJ red-squiggles the @Override for not overriding and the method signature for the same reason as the error message. 由于与错误消息相同的原因,IntelliJ红色扭曲了@Override,因为没有覆盖和方法签名。
For lack of knowing what else to call it, my XML file is called "cascade.properties", is under src > main > java > foo.bar > resources, and looks like: 由于不知道还有什么叫它,我的XML文件名为“cascade.properties”,位于src> main> java> foo.bar> resources下,如下所示:
<mongo:mongo-client id="mongoClient" host="localhost" />
<mongo:db-factory id="mongoDbFactory" dbname="test" mongo-ref="mongoClient" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.baeldung.event.CustomerCascadeSaveMongoEventListener" />
Finally, inside my Customer class, the field referencing the address looks like: 最后,在我的Customer类中,引用该地址的字段如下所示:
@DBRef
@NotNull
private Address address;
The main error message is as follows: 主要错误消息如下:
Error:(16, 17) java: name clash: onBeforeConvert(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) in io.catalyte.training.CustomerCascadeSaveMongoEventListener and onBeforeConvert(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) in org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener have the same erasure, yet neither overrides the other 错误:(16,17):io.catalyte.training.CustomerCascadeSaveMongoEventListener和onBeforeConvert(org.springframework.data.mongodb.core)中的java:name clash:onBeforeConvert(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener中的.mapping.event.BeforeConvertEvent)具有相同的擦除,但都没有覆盖其他
I think solving that problem would also eliminate the second error message: 我认为解决该问题也会消除第二条错误消息:
Error:(15, 5) java: method does not override or implement a method from a supertype 错误:(15,5)java:方法不会覆盖或实现超类型的方法
While my configuration file may well have nothing to do with the error(s), I would appreciate pointers if anything about its contents will not support or is unnecessary for the cascading save behavior I'm going for. 虽然我的配置文件可能与错误无关,但如果有关于其内容的任何内容不支持或不需要我想要的级联保存行为,我将不胜感激。 So far, it's contents were written only with the intent of supporting this functionality. 到目前为止,它的内容只是为了支持这个功能而编写的。
Though it's unlikely anyone else is going to have exactly this issue, for posterity: 对于后人来说,虽然不可能有其他任何人确实会遇到这个问题:
As one could have guessed, there was nothing wrong with Baeldung's code, just how I copied it. 正如人们可能已经猜到的那样,Baeldung的代码没有任何问题,只是我如何复制它。 My mistakes were two, both having to do with the class signature. 我的错误是两个,都与班级签名有关。 It should have looked like this: 它应该看起来像这样:
public class CustomerCascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {
without the annotation I gave it. 没有我给它的注释。
Also for posterity, Java config files like MongoConfig are often in the main package in a sub-package called "config". 同样对于后人来说,像MongoConfig这样的Java配置文件通常位于名为“config”的子包中的主程序包中。 To get this functionality to work, my MongoTransactionConfig file looked like this: 要使此功能起作用,我的MongoTransactionConfig文件如下所示:
@Configuration
@EnableMongoRepositories(basePackages = "io.catalyte.training.repositories")
public class MongoTransactionConfig extends AbstractMongoConfiguration {
@Bean
public CustomerCascadeSaveMongoEventListener customerCascadingMongoEventListener() {
return new CustomerCascadeSaveMongoEventListener();
}
@Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
@Override
protected String getDatabaseName() {
return "test";
}
@Override
public MongoClient mongoClient() {
return new MongoClient("127.0.0.1", 27017);
}
}
though you or your IDE will need to provide the appropriate package and import statements. 虽然您或您的IDE需要提供适当的包和import语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.