春天:一个类可以同时是@Document和@Table吗? [英] Spring : Can a class be both @Document and @Table
问题描述
我在MVC上下文中使用spring.我正在使用Cassandra和MonogDB.对于项目,我必须使我的模型和dao类与这两个数据库兼容. @Document
和@Table
是否可能具有相同的模型类? spring能够在运行时做出区分吗?
I am using spring in MVC context. I am working with Cassandra and MonogDB. For the project, I have to make my models and dao classes compatible with both databases. Is it possible to have the same model class be @Document
and also be @Table
? Will spring be able to make the distinction at runtime?
例如,我将拥有mongo
For example, for mongo i will have
@Document
public class Book{
@Id
private String uniqueId;
/*Other details*/
}
对于卡桑德拉,我将拥有
and for cassandra i will have
@Table
public class Book{
@PrimaryKeyColumn(name = "uniqueId", ordinal = 0, type = PrimaryKeyType.PARTITIONED, ordering = Ordering.ASCENDING)
private String uniqueId;
/*Other details*/
}
我可以给类似
@Table
@Document
public class Book{
@PrimaryKeyColumn(name = "uniqueId", ordinal = 0, type = PrimaryKeyType.PARTITIONED, ordering = Ordering.ASCENDING)
@Id
private String uniqueId;
/*Other details*/
}
推荐答案
是的,这很有可能.一个经典的例子是当您希望在本地/测试和生产环境中拥有不同的数据源时.您可以在测试环境中使用Mongo,在生产环境中使用MySql.因此,您可以同时使用spring-data-mongo
和javax.persistance
批注
Yes it is very much possible. One classic example is when you want to have different datasources in your local/test and production environments. You could use Mongo in your test environment and MySql in production. So you could use both spring-data-mongo
and javax.persistance
annotations simultaneosly
这篇关于春天:一个类可以同时是@Document和@Table吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!