robdbirch (8) [Avatar] Offline
#1
These are things I did to get the JPA example working.
It is possible that not everything specified is needed, but this is what I have working, and I am not an expert I am learning about Spring Boot.
I have placed this code in GitHub. There's a JDBC branch and a JPA Branch: https://github.com/robdbirch/spia5/tree/jpa

WARNING My schema names and some other naming in objects don't exactly match the book, but I am sure you will get the gist of it.

Application Properties:
File: application.properties

Property: spring.jpa.hibernate.ddl-auto=none


Schema Changes:
File: schema.sql

All camel cased column names are changed to snake case.
For example:
CHANGE:
placedAt
TO
placed_at

It appears JPA by default maps camel cased Java naming to snake cased SQL naming.
It seems this can be changed be creating a JPA naming strategy.

Entities:
Changed all entities to the annotation Generation Type of
IDENTITY


Taco Entity:
Made serializable. My thinking is that if an Order entity holds a list of Tacos and it stores it in a session, then I would guess that the
Taco
would need to be serializable also?

Explicitly specified the join columns to generate the correct SQL

@Size(min=1, message="You must choose at least 1 ingredient")
	@JoinTable(name="Taco_Ingredient",
				joinColumns = @JoinColumn(name="taco_id", referencedColumnName="id"),
				inverseJoinColumns = @JoinColumn(name="ingredient_id", referencedColumnName="id")
	)
	@ManyToMany(targetEntity=Ingredient.class)
	private List<Ingredient> ingredients;


Order Entity:
Created a default constructor where I initialized the list of Tacos to an empty ArrayList:

private List<Taco> tacos;
	
	public Order( ) {
		this.tacos = new ArrayList<>();
	}


Specified the join columns:

@ManyToMany(targetEntity=Taco.class)
	@JoinTable(name="Taco_Order_Tacos",
		joinColumns = @JoinColumn(name="order_id", referencedColumnName="id"),
		inverseJoinColumns = @JoinColumn(name="taco_id", referencedColumnName="id")
	)