I have pretty much zero experience with Hibernate, though I've used similar persistence libraries in other languages before. I'm working on a Java project that will require a way to define "models" (in the MVC sense) in text configuration files, generate the database tables automatically, and (ideally) be database-backend-agnostic. As far as I can tell from some quick Googling, Hibernate is the only widely-used backend-agnostic Java database library; while I could write my own compatibility layer between my model system and multiple DB backends, that's a debugging endeavor that I'd like to avoid if possible.
我对Hibernate几乎没有经验,尽管之前我在其他语言中使用过类似的持久性库。我正在开发一个Java项目,它需要一种方法来定义文本配置文件中的“模型”(在MVC意义上),自动生成数据库表,并且(理想情况下)是数据库后端不可知的。据我所知,从一些快速的谷歌搜索,Hibernate是唯一广泛使用的后端不可知的Java数据库库;虽然我可以在我的模型系统和多个数据库后端之间编写自己的兼容层,但这是我想尽可能避免的调试工作。
My question is: Can Hibernate be used to store data whose structure is represented in some other way than an annotated Java class file, such as a HashMap with some configuration object that describes its structure? And, if not, are there any other relatively-stable Java database libraries that could provide this functionality?
我的问题是:Hibernate可以用来存储其结构以带注释的Java类文件以外的其他方式表示的数据,例如带有描述其结构的配置对象的HashMap吗?如果没有,是否有其他相对稳定的Java数据库库可以提供此功能?
EDIT: Here's a clearer description of what I'm trying to accomplish:
编辑:这里有一个更清楚的描述我正在努力实现的目标:
I am writing a data-modeling library. When a certain method of the library is called, and passed a configuration object (loaded from a text file), it should create a new "model," and create a database table for that model if necessary. The library can be queried for items of this model, which should return HashMaps containing the models' fields. It should also allow HashMaps to be saved to the database, provided their structure matches the configuration files. None of these models should be represented by actual compiled Java classes.
我正在写一个数据建模库。当调用库的某个方法并传递配置对象(从文本文件加载)时,它应该创建一个新的“模型”,并在必要时为该模型创建数据库表。可以查询库以查找此模型的项目,该项目应返回包含模型字段的HashMaps。它还应该允许将HashMaps保存到数据库,前提是它们的结构与配置文件匹配。这些模型都不应该由实际编译的Java类表示。
3 个解决方案
#1
4
I think you could try use @MapKey annotation provided by JPA (not the Hibernate @MapKey annotation, it's pretty different!).
我想你可以尝试使用JPA提供的@MapKey注释(不是Hibernate @MapKey注释,它是非常不同的!)。
@javax.persistence.OneToMany(cascade = CascadeType.ALL)
@javax.persistence.MapKey(name = "name")
private Map<String, Configuration> configurationMap = new HashMap<String, Configuration>();
#2
1
I don't believe Hibernate will let you have a Map as an @Entity but it will let you have a custom class that contains a map field:
我不相信Hibernate会让你有一个Map作为@Entity,但它会让你有一个包含map字段的自定义类:
@Entity
public class Customer {
@Id @GeneratedValue public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Integer id;
@OneToMany @JoinTable(name="Cust_Order")
@MapKeyColumn(name"orders_number")
public Map<String,Order> getOrders() { return orders; }
public void setOrders(Map<String,Order> orders) { this.orders = orders; }
private Map<String,Order> orders;
}
(example from Hibernate docs)
(来自Hibernate docs的例子)
Additionally, you don't have to use annotations (if that is what you're trying to avoid): Hibernate relationships can be described via xml files and there are utilities (maven plugins for example) which can automatically generate the necessary java pojo's from the xml.
此外,您不必使用注释(如果这是您要避免的):可以通过xml文件描述Hibernate关系,并且有一些实用程序(例如maven插件)可以自动生成必要的java pojo。 xml。
Does your model require a relational database? You might consider a database like Mongo that stores any object you can represent with JSON.
您的模型是否需要关系数据库?您可以考虑使用像Mongo这样的数据库来存储您可以使用JSON表示的任何对象。
#3
0
you can configure hibernate to work without any entity classes (beans linked to tables), 1. you need to use xml configuration for this. in place of class
use entity-name
and in place of <property name="something"
use <property node="something"
.
您可以将hibernate配置为在没有任何实体类(链接到表的bean)的情况下工作,1。您需要使用xml配置。代替类使用entity-name而不是
- create a hibernate session with entiy-mode as map.
- 使用entiy-mode创建一个hibernate会话作为map。
you can use a map to store and retuive information from db. Remember, since you are using map there will be difficulties in 2-way mapping (this was as of 3.3, not sure if its been fixed in later releses)
您可以使用映射来存储和返回来自db的信息。请记住,因为你正在使用地图,所以在双向映射方面会有困难(这是从3.3开始,不确定它是否在以后的版本中被修复)
#1
4
I think you could try use @MapKey annotation provided by JPA (not the Hibernate @MapKey annotation, it's pretty different!).
我想你可以尝试使用JPA提供的@MapKey注释(不是Hibernate @MapKey注释,它是非常不同的!)。
@javax.persistence.OneToMany(cascade = CascadeType.ALL)
@javax.persistence.MapKey(name = "name")
private Map<String, Configuration> configurationMap = new HashMap<String, Configuration>();
#2
1
I don't believe Hibernate will let you have a Map as an @Entity but it will let you have a custom class that contains a map field:
我不相信Hibernate会让你有一个Map作为@Entity,但它会让你有一个包含map字段的自定义类:
@Entity
public class Customer {
@Id @GeneratedValue public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Integer id;
@OneToMany @JoinTable(name="Cust_Order")
@MapKeyColumn(name"orders_number")
public Map<String,Order> getOrders() { return orders; }
public void setOrders(Map<String,Order> orders) { this.orders = orders; }
private Map<String,Order> orders;
}
(example from Hibernate docs)
(来自Hibernate docs的例子)
Additionally, you don't have to use annotations (if that is what you're trying to avoid): Hibernate relationships can be described via xml files and there are utilities (maven plugins for example) which can automatically generate the necessary java pojo's from the xml.
此外,您不必使用注释(如果这是您要避免的):可以通过xml文件描述Hibernate关系,并且有一些实用程序(例如maven插件)可以自动生成必要的java pojo。 xml。
Does your model require a relational database? You might consider a database like Mongo that stores any object you can represent with JSON.
您的模型是否需要关系数据库?您可以考虑使用像Mongo这样的数据库来存储您可以使用JSON表示的任何对象。
#3
0
you can configure hibernate to work without any entity classes (beans linked to tables), 1. you need to use xml configuration for this. in place of class
use entity-name
and in place of <property name="something"
use <property node="something"
.
您可以将hibernate配置为在没有任何实体类(链接到表的bean)的情况下工作,1。您需要使用xml配置。代替类使用entity-name而不是
- create a hibernate session with entiy-mode as map.
- 使用entiy-mode创建一个hibernate会话作为map。
you can use a map to store and retuive information from db. Remember, since you are using map there will be difficulties in 2-way mapping (this was as of 3.3, not sure if its been fixed in later releses)
您可以使用映射来存储和返回来自db的信息。请记住,因为你正在使用地图,所以在双向映射方面会有困难(这是从3.3开始,不确定它是否在以后的版本中被修复)