Một câu hỏi followup cho câu hỏi trước đây của tôi: Generate an SQL DB creation script with Hibernate 4Hibernate SchemaExport và Persistence đơn vị
Mục đích là để có một công cụ dòng lệnh có thể tạo ra một tập tin với schema SQL của một đơn vị kiên trì nhất định (tương tự như hibernatetool- hbm2ddl Ant nhiệm vụ hiện diện trong các công cụ Hibernate).
Điều này, theo câu trả lời cho câu hỏi trước của tôi, có thể đạt được với org.hibernate.tool.hbm2ddl.SchemaExport
.
Thay vì thêm tất cả các thực thể vào Configuration
(như được đề xuất trong câu trả lời trước) tôi muốn chỉ định một PersistenceUnit
.
Có thể thêm đơn vị liên tục vào Hibernate Configuration
?
Something như
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
...
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("persistentUnitName", properties);
Configuration configuration = new Configuration();
... missing part ...
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.setOutputFile("schema.sql");
...
Sửa theo yêu cầu trong các ý kiến một mẫu persistence.xml
. Mỗi lớp được chú thích với @Entity
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0"
>
<persistence-unit
name="doiPersistenceUnit"
transaction-type="JTA"
>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/doi</jta-data-source>
<class>ch.ethz.id.wai.doi.bo.Doi</class>
[...]
<class>ch.ethz.id.wai.doi.bo.DoiPool</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.connection.characterEncoding" value="utf8" />
<property name="hibernate.connection.charSet" value="utf8" />
</properties>
</persistence-unit>
</persistence>
để bạn muốn lưu 'config.addAnnotatedClass (MyMappedPojo1.class);' dòng? – yair
@yair yes Tôi muốn tránh chỉ định tất cả các lớp theo cách thủ công (và tránh mã hóa chúng một cách thủ công). Tôi biết tôi có thể phân tích cú pháp tệp persistence.xml nhưng tôi nghi ngờ có một cách dễ dàng hơn. – Matteo
Tôi nghĩ rằng bạn đang thiếu cũng đi qua phương ngữ vào cấu hình - nó sẽ thất bại khi SchemaExport được tạo ra. – kboom