我尝试使用JPA 2.1和Hibernate 5.0生成一个DDL,但不知何故我做错了,它忽略了inverseJoinColumn
定义中外键的名称。
这些是我的实体:
父:
@Entity
@Table(name = "PARENT", indexes = { @Index(columnList = "NAME", name = "IDX_NAME") }, uniqueConstraints = { @UniqueConstraint(columnNames = "NAME", name = "UK_NAME") })
@SequenceGenerator(allocationSize = 1, name = "PARENT_ID_GENERATOR", sequenceName = "PARENT_ID_SEQ")
public class Parent {
@Id
@GeneratedValue(generator = "PARENT_ID_GENERATOR", strategy = SEQUENCE)
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "PARENT_CHILD", joinColumns = @JoinColumn(name = "PARENT_ID"), foreignKey = @ForeignKey(name ="FK_PARENT_CHILD_PARENT"), inverseJoinColumns = @JoinColumn(name = "CHILD_ID", foreignKey = @ForeignKey(name ="FK_ABC")), uniqueConstraints = {@UniqueConstraint(columnNames = "CHILD_ID", name = "UK_CHILD_ID") })
private List<Child> children = new ArrayList<>();
}
子:
@Entity
@Table(name = "CHILD", indexes = { @Index(columnList = "NAME", name = "IDX_NAME") }, uniqueConstraints = { @UniqueConstraint(columnNames = "NAME", name = "UK_NAME") })
@SequenceGenerator(allocationSize = 1, name = "CHILD_ID_GENERATOR", sequenceName = "CHILD_ID_SEQ")
public class Child {
@Id
@GeneratedValue(generator = "CHILD_ID_GENERATOR", strategy = SEQUENCE)
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
}
我编写了以下小型主类来生成ddl
SchemaCreator:
public class SchemaCreator {
public static void main(String[] args) throws IOException {
execute("default", "./target/create.sql");
System.exit(0);
}
public static void execute(String persistenceUnitName, String destination) {
System.out.println("Generating DDL create script to : " + destination);
final Properties persistenceProperties = new Properties();
persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, "");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata");
persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination);
persistenceProperties.setProperty(AvailableSettings.JDBC_DRIVER,"org.h2.Driver");
persistenceProperties.setProperty(AvailableSettings.JDBC_URL,"jdbc:h2:mem:jpaschema");
Persistence.generateSchema(persistenceUnitName, persistenceProperties);
}
}
我的persistence.xml看起来像这样
持续
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="default">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>nl.generate.ddl.example.Child</class>
<class>nl.generate.ddl.example.Parent</class>
</persistence-unit>
有人能指出我正确的方向吗? 我在github https://github.com/vdmc/generate-ddl-example.git
的示例项目中包含了上述代码答案 0 :(得分:2)
实际上,元数据中有一个不正确的地方是@JoinTable
上有&#34; inverseForeignKey&#34; 。在那里指定您的FK信息,而不是在&#34; inverseJoinColumns&#34; 下。
@JoinTable(name = "PARENT_CHILD",
joinColumns = @JoinColumn(name = "PARENT_ID"),
foreignKey = @ForeignKey(name ="FK_PARENT_CHILD_PARENT"),
inverseJoinColumns = @JoinColumn(name = "CHILD_ID"),
inverseForeignKey = @ForeignKey(name ="FK_ABC"),
uniqueConstraints = {@UniqueConstraint(columnNames = "CHILD_ID", name = "UK_CHILD_ID") })