Page tree

Michael O'Brien

Skip to end of metadata
Go to start of metadata

History

https://eclipsejpa.blogspot.com/2015/03/jpa-20-map-relationships.html

Theory

Implementations

http://wiki.eclipse.org/EclipseLink/Examples/JPA

EclipseLink Maven Configuration

pom.xml
 
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.4.13.Final</version>
  <type>pom</type>
</dependency>
       


Hibernate

https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html

https://docs.jboss.org/tools/latest/en/hibernatetools/html/reverseengineering.html

Hibernate Maven Configuration

Hibernate 5.4 supports JPA 2.2

https://bintray.com/hibernate/artifacts/hibernate-orm/5.4.13.Final

pom.xml
        <hibernate.version>5.3.7.Final</hibernate.version>


JPA Code and DB Generation

ActionProviderVersionJPAAPI
Schema to EntityHibernate5.42.2hbm2java
Schema to EntityEclipseLink

JPA DDL
Entity to SchemaHibernate

JPA persistence unit DDL
Entity to SchemaEclipseLink
2.2JPA persistence unit DDL

Java to DB Schema Generation

JPA 2.1 Schema Generation

The JPA 2.1 specification update expanded on proprietary EclipseLink and Hibernate generation 

Hibernate DB Generation

https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/schema/Schema.html

Example schema generation using Hibernate

hbm2ddl

Add to your persistence.xml

<property name="hibernate.hbm2ddl.auto" value="create-drop"/>


EclipseLink DB Generation

http://wiki.eclipse.org/EclipseLink/Examples/JPA/DDL

Schema to Java Code Generation

Hibernate hbm2java Entity Generation

https://docs.jboss.org/tools/latest/en/hibernatetools/html/reverseengineering.html

Override Entity Constructor Ordering during hbm2java generation

Copy PojoConstructor.ftl and modify the freemarker code to handle your particular requirements.

https://github.com/axiom-data-science/hibernate-tools-dto/blob/master/src/main/resources/pojoWithDTO/PojoConstructors.ftl

https://freemarker.apache.org/docs/ref_builtins_loop_var.html and https://freemarker.apache.org/docs/dgui_misc_userdefdir.html https://freemarker.apache.org/docs/ref_directive_if.html

EclipseLink Entity Generation


JPA Data Population on EM startup

JPA 2.1 defines the following property that can be used to bootstrap a DB with pre-populated data.

javax.persistence.sql-load-script-source


FAQ

Owning Side of the Mapping

The M:1 side of a  OneToMany mapping is the owning side with a corresponding join column. 

For example see the code for the JPA 2.0 metamodel API in https://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_96:_20091019:_Attribute.getPersistentAttributeType.28.29_treats_ManyToOne_the_same_as_OneToOne

@Entity(name="BoardMetamodel")
@Table(name="CMP3_MM_BOARD")
public class Board implements java.io.Serializable{
    // The M:1 side is the owning side for "circuitBoards"
    @ManyToOne(fetch=EAGER)
    @JoinTable(name="CMP3_MM_COMPUTER_MM_BOARD", 
            joinColumns = @JoinColumn(name="BOARD_ID"), 
            inverseJoinColumns = @JoinColumn(name="COMPUTER_ID"))   
    private Computer computer;
}
 
@Entity(name="ComputerMetamodel")
@Table(name="CMP3_MM_COMPUTER")
public class Computer implements java.io.Serializable {
    // Inverse side 
    @OneToMany(cascade=ALL, mappedBy="computer")
    private Collection<Board> circuitBoards;
}
  • No labels