Last active
August 24, 2018 11:52
-
-
Save gsitgithub/8a5edcd9655c6e47faa4887bac32cc55 to your computer and use it in GitHub Desktop.
JPA mapping tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Start spring-boot app without depending on Database? | |
https://stackoverflow.com/questions/23850585/how-to-start-spring-boot-app-without-depending-on-database/23875516#23875516 | |
Spring boot apps will start fine even if DB is down or there is no DB. | |
Apps will pick up the connections on the fly as DB comes up which means there is no need to restart the web server or redeploy the apps. | |
There is no need to start the tomcat or redeploy the apps, if DB goes down from running state and comes up again. | |
application.yml : | |
spring: | |
datasource: | |
driverClassName: com.mysql.jdbc.Driver | |
url: jdbc:mysql://localhost:3306/schema | |
username: root | |
password: root | |
continueOnError: true | |
initialize: false | |
initialSize: 0 | |
timeBetweenEvictionRunsMillis: 5000 | |
minEvictableIdleTimeMillis: 5000 | |
minIdle: 0 | |
jpa: | |
show-sql: true | |
hibernate: | |
ddl-auto: none | |
naming_strategy: org.hibernate.cfg.DefaultNamingStrategy | |
properties: | |
hibernate: | |
dialect: org.hibernate.dialect.MySQL5Dialect | |
hbm2ddl: | |
auto: none | |
temp: | |
use_jdbc_metadata_defaults: false | |
I had to do the same with EclipseLink, and all it took to do it was <property name="eclipselink.validation-only" value="true"/> in persistence.xml | |
==================================================== | |
Don’t use unidirectional one-to-many associations | |
Avoid the mapping of huge to-many associations | |
better use an unidirectional many-to-one association | |
Think twice before using CascadeType.Remove | |
Use orphanRemoval when modeling parent-child associations | |
Implement helper methods to update bi-directional associations | |
Define FetchType.LAZY for @ManyToOne association | |
You can have a uni-directional mapping between Teams and Matches as follows: | |
In Matches Class: | |
@ManyToOne(optional=false) | |
@JoinColumn(name="home_team_id", referencedColumnName="team_id") | |
private Team homeTeam; | |
@ManyToOne(optional=false) | |
@JoinColumn(name="away_team_id" referencedColumnName="team_id") | |
private Team awayTeam; | |
If you need a bi-directional relationship you can add the following : | |
In Teams Class : | |
@OneToMany(mappedBy="homeTeam") | |
private Set<Matches> homeMatches; | |
@OneToMany(mappedBy="awayTeam") | |
private Set<Matches> awayMatches; | |
=========================== | |
@Entity | |
public class Person implements Serializable { | |
@Id Integer id; | |
@OneToOne | |
@JoinColumn(name = "id") | |
Job myJob; | |
} | |
@Entity | |
public class Job implements Serializable { | |
@Id Integer id; | |
@OneToOne(mappedBy = "myJob") | |
Person currentWorker; | |
} | |
(pay attemption to remove duplicated colum 'person_id' from Job) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment