How to cascade insert - java

UPDATE 2014-01-29: I have changed relationshis so entity code and error changed too
I have to store some data in a database and my relations are like this:
Order3d 1-----* Draw 1-----* Line
I want to save everything like this:
Order3d o=new Order3d();
//add data to order (lines and draws)
Session s=HibernateUtils.getSessionFactory().openSession();
s.save(o);
s.close();
I don't have runtime error but now my line is not inserted (only order3d and drawfile are saved)
here are my entities (obviously, they are POJO so I didn't copy getters and setters):
#Entity
#Table (name="order3d")
public class Order3d implements Serializable {
private static final long serialVersionUID = -2241346447352903470L;
public enum State {DEMAND, ESTIMATED, PAYED, PENDING, PRODUCED, SENT, DELIVERED};
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column (name="id")
private int id;
#Column (name="person")
private int person;
#Column (name="state", columnDefinition="smallint")
private State state;
#Column (name="labor_expense")
private float laborExpense=0;
#Column (name="travel_expense")
private float travelExpense=0;
#Column (name="validity_date")
private Date validityDate;
#Column (name="demand_date")
private Date demandDate;
#Column (name="estimate_date")
private Date estimateDate;
#Column (name="order_date")
private Date orderDate=null;
#Column (name="modification", columnDefinition="TEXT")
private String modification;
#Column (name="delivery", columnDefinition = "BIT", length = 1)
private Boolean delivery=true;
#OneToMany(mappedBy="order3d", cascade=CascadeType.ALL)
private Set<DrawFile> myDraws=new HashSet<DrawFile>(5);
public void addDrawFile(DrawFile df) {
df.setOrder3d(this);
myDraws.add(df);
}
}
#Entity
#Table (name="draw")
public class DrawFile implements Serializable {
private static final long serialVersionUID = -9024754876558087847L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column (name="id")
private int id;
#Column (name="hashname", columnDefinition="CHAR(64)")
private String hashname;
#Column (name="filename")
private String filename="";
#Column (name="accepted", columnDefinition = "BIT", length = 1)
private Boolean accepted=true;
#Column (name="format")
private String format;
#Column (name="size")
private int size;
#Column(name="width")
short width;
#Column(name="depth")
short depth;
#Column(name="height")
short height;
#OneToMany(mappedBy="draw", cascade=CascadeType.ALL)
private Set<Order3dLine> myLines=new HashSet<Order3dLine>(5);
#ManyToOne
#JoinColumn(name="order3d_id")
private Order3d order3d;
public void addLine(Order3dLine l) {
l.setDraw(this);
myLines.add(l);
}
}
#Entity
#Table (name="line3d")
public class Order3dLine implements Serializable {
private static final long serialVersionUID = 3993578603382571145L;
#NaturalId
#ManyToOne
#JoinColumn(name="draw_id")
private DrawFile draw;
#Column (name="quantity")
private short quantity=0;
#Id
#Column (name="material")
private String material;
#Id
#Column (name="color")
private int color;
#Column(name="produced")
short produced=0;
#Column(name="duration")
short duration=0;
}
Here are my MySQL tables:
CREATE TABLE `draw` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) DEFAULT NULL,
`format` varchar(6) DEFAULT NULL,
`hashname` char(64) DEFAULT NULL,
`accepted` bit(1) NOT NULL DEFAULT b'0',
`size` int(11) NOT NULL DEFAULT '0',
`order3d_id` int(11) NOT NULL,
`width` smallint(6) NOT NULL DEFAULT '0',
`depth` smallint(6) NOT NULL DEFAULT '0',
`height` smallint(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
CONSTRAINT `fk_draw_order3d` FOREIGN KEY (`id`) REFERENCES `order3d` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf-8;
/*!40101 SET character_set_client = #saved_cs_client */;
CREATE TABLE `line3d` (
`draw_id` int(11) NOT NULL,
`material` varchar(10) NOT NULL,
`color` int(11) NOT NULL,
`quantity` smallint(6) NOT NULL DEFAULT '0',
`produced` smallint(6) NOT NULL DEFAULT '0',
`duration` smallint(6) NOT NULL DEFAULT '0',
`layers` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`draw_id`,`material`,`color`),
CONSTRAINT `fk_line_draw` FOREIGN KEY (`draw_id`) REFERENCES `draw` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
CREATE TABLE `order3d` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`person` int(11) DEFAULT NULL,
`state` smallint(6) DEFAULT NULL,
`labor_expense` float DEFAULT NULL,
`travel_expense` float DEFAULT NULL,
`validity_date` date DEFAULT NULL,
`estimate_date` date DEFAULT NULL,
`order_date` date DEFAULT NULL,
`delivery` bit(1) NOT NULL DEFAULT b'0',
`modification` text,
`demand_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_order_user` (`person`),
CONSTRAINT `fk_order_user` FOREIGN KEY (`person`) REFERENCES `person` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf-8;

(1) You have a bi-directional relation between order and order line.
Hibernate expects you to assign order to line like in this convenience method:
#Entity
#Table(name="ORDERS")
public class Order {
[...]
public void addLine(OrderLine line) {
line.setOrder(this);
myLines.add(line);
}
}
See also this thread.
(2) "order" is not accepted as an valid table table name. Choose for example "orders".
(3) You forgot the cascade option on your one-to-one relation between order line and draw. Consider using #Embedded instead of #OneToOne. See Hibernate Doc
I put together minimal working example:
Order:
#Entity
#Table(name = "ORDERS")
public class Order {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column (name = "id")
private Long id;
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name="line_id", nullable = false)
private Set<OrderLine> lines = new HashSet<OrderLine>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<OrderLine> getLines() {
return lines;
}
public void setLines(Set<OrderLine> lines) {
this.lines = lines;
}
public void addLine(OrderLine line) {
line.setOrder(this);
lines.add(line);
}
}
Order line:
#Entity
#Table(name = "ORDER_LINES")
public class OrderLine {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column (name = "id")
private Long id;
#ManyToOne(fetch = FetchType.EAGER)
private Order order;
#Column(name = "content")
private String content;
#Embedded
private OrderDraw draw;
public OrderDraw getDraw() {
return draw;
}
public void setDraw(OrderDraw draw) {
this.draw = draw;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
Order Draw (whatever this is)
#Embeddable
public class OrderDraw {
private int width;
private int height;
public OrderDraw() {
this(0,0);
}
public OrderDraw(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}

Related

Spring JPA InvalidDataAccessApiUsageException Error

I'm not sure why I'm getting this error. I'm trying to write a test and I'm getting this error on line artistLabelRepository.save(artistLabel1); in ArtistLabelRepositoryTestIT
Can not set com.pandora.label.entity.Label field com.pandora.label.entity.ArtistLabelId.label to java.lang.Long
These are the tables. the tables are already created and populated. I can alter the tables, but would like to not delete them and recreate them.
CREATE TABLE label (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
parent_label_id BIGINT REFERENCES label(id)
)
CREATE INDEX album_uid_idx ON album_label(id);
CREATE TABLE artist_label (
artist_id VARCHAR(60) NOT NULL,
label_id BIGINT NOT NULL,
PRIMARY KEY (artist_id, label_id)
);
CREATE INDEX artist_label_idx ON artist_label(label_id);
ALTER TABLE artist_label ADD CONSTRAINT artist_label_fk FOREIGN KEY (label_id) REFERENCES label(id);
these are the Entities
class ArtistLabelId implements Serializable {
private String artistId;
private Label label;
public ArtistLabelId() {
// no arg constructor for JPA
}
public ArtistLabelId(String artistId, Label label) {
this.artistId = artistId;
this.label = label;
}
// setters and getters
}
#Entity
#IdClass(ArtistLabelId.class)
#Table(name="artist_label",
indexes = {#Index(name = "artist_label_idx", columnList = "artist_id")})
public class ArtistLabel {
#Id
#Column (name = "artist_id")
private String artistId;
#Id
#ManyToOne
#JoinColumn(name = "label_id")
private Label label;
public ArtistLabel() {
// no arg constructor for JPA
}
public ArtistLabel(String artistId, Label label) {
this.artistId = artistId;
this.label = label;
}
// setters and getters
}
#Entity
#Table(name="label")
public class Label {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator="label_generator")
#SequenceGenerator(name= "label_generator", sequenceName = "label_id_seq", allocationSize = 1)
#Column(name = "id")
private long labelId;
#Column (name = "name")
private String name;
#ManyToOne
#JoinColumn(name = "parent_label_id")
private Label parentLabel; //references parent label
#Column(name = "create_date")
private Timestamp createDate;
#Column(name = "last_modified")
private Timestamp lastModified;
public Label(){
//no-arg constructor for JPA
}
public Label(String name, Label parentLabel){
this.name = name;
this. parentLabel = parentLabel;
}
public Label(long labelId, String name, Label parentLabel){
this.labelId = labelId;
this.name = name;
this.parentLabel = parentLabel;
}
// setters and getters
}
this is the repository
#Repository
public interface ArtistLabelRepository extends CrudRepository<ArtistLabel, Long> {}
and this is the test. i'm trying to save an artistLabel artistLabelRepository.save(artistLabel1);
#ActiveProfiles(value="test")
#SpringBootTest(classes = {Application.class})
public class ArtistLabelRepositoryTestIT {
#Autowired
ArtistLabelRepository artistLabelRepository;
#Autowired
LabelRepository labelRepository;
#Test
public void testFindByLabelId(){
Label label = new Label(123L, "label1", null);
labelRepository.save(label);
ArtistLabel artistLabel1 = new ArtistLabel("artistId1", label);
artistLabelRepository.save(artistLabel1);
}
}

How to map a OneToOne with ZeroOrOneToOne back reference in JPA?

I have a legacy app which has an entity relationship that looks like this. I changed the names of the fields to less realistic values and reduced to only the relevant fields.
CREATE TABLE `billing_target` (
`billingTargetID` int(11) NOT NULL,
`targetType` char(5) NOT NULL,
`targetID` int(11) NOT NULL
PRIMARY KEY (`billingTargetID`)
);
CREATE TABLE `Client` (
`clientID` int(11) NOT NULL,
`name` varchar(200),
`color` varchar(200),
`shape` varchar(200)
PRIMARY KEY (`clientID`),
CONSTRAINT `fk_client_billingTarget`
FOREIGN KEY (`clientID`)
REFERENCES `billing_target` (`targetID`)
);
My most current attempt which causes an issue when saving as it gives a null entity key exception.
#Data
#Entity
public class BillingTarget implements Serializable {
#Id
#Column(name = "billingTargetID")
private Integer id;
#Column(name = "targetID", nullable = false)
private Integer targetID;
#Column(name = "targetType", nullable = false)
private String type;
}
#Data
#Entity
public class Client implements Serializable {
#Id
#Column(name = "clientID")
private Integer id;
#OneToOne
#JoinColumn(name = "clientID")
#MapsId("targetID")
private BillingTarget billingTarget;
private String name;
private String color;
private String shape;
}
Here's the PlantUML code if interested
#startuml
hide circle
entity BillingTarget {
* billingTargetID <<generated>>
--
* targetType
* targetID <<unique>>
}
entity Client {
* clientID <<fk>>
--
* name
color
shape
}
BillingTarget ||--o| Client : "targetID:clientID"
#enduml
I was already thinking of using a MappedSuperclass but right now it is only one type (though it could be more). Second the billing target may be zero and not null as there's a NOT NULL constraint already present.

Why this Hibernate Relation isn't working?

I try to Connect Pizzas and Ingredients in a n:m relation while all Pizzas have Ingredients as an Attribute List of Ingredients. But in the Relationstable when I create a new Pizza and try to commit there is an Error with the PizzaID in the Relationtable.
The relational Table:
CREATE TABLE `Pizza_Ingredience_Relation` (
`PizzaID` int(11) NOT NULL,
`IngredientID` int(11) NOT NULL,
`Amount` int(11) NOT NULL,
`Volume_Unit` varchar(1) NOT NULL,
PRIMARY KEY (`PizzaID`,`IngredientID`),
KEY `FKc58en2gx5a8n1swmu9tda345` (`IngredientID`),
CONSTRAINT `FK_IngredienceId` FOREIGN KEY (`IngredientID`) REFERENCES `Zutatenliste` (`ID`),
CONSTRAINT `FKc58en2gx5a8n1swmu9tda345` FOREIGN KEY (`IngredientID`) REFERENCES `Zutatenliste` (`ID`),
CONSTRAINT `FKhghfxg8raskdydyu8o8msxtfn` FOREIGN KEY (`PizzaID`) REFERENCES `Pizza` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
The Ingredient Table:
CREATE TABLE `Zutatenliste` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(20) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
The Pizza Table:
CREATE TABLE `Pizza` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(20) NOT NULL,
`PreisKlein` double NOT NULL,
`PreisMittel` double NOT NULL,
`PreisGroß` double NOT NULL,
`PreisFamilie` double NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
I have two hibernate Entitites, one is a Pizza Entitiy and one the Ingredient Entitiy:
package Model.PizzenDB.SQLConnectionClasses.MySQL;
import Model.PizzenDB.Pizza;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.Where;
import javax.persistence.*;
import java.util.LinkedList;
import java.util.Set;
#Entity
#Table(name = "Pizza")
public class MySQLPizzaHibernateEntity {
#Id
#Column(name = "ID")
private int id;
#Column(name = "Name")
private String name;
#Column(name = "PreisKlein")
private double smallPrice;
#Column(name = "PreisMittel")
private double middlePrice;
#Column(name = "PreisGroß")
private double bigPrice;
#Column(name = "PreisFamilie")
private double familyPrice;
#ManyToMany(cascade = { CascadeType.ALL })
#JoinTable(
name = "Pizza_Ingredience_Relation",
joinColumns = { #JoinColumn(name = "PizzaID", referencedColumnName = "ID") },
inverseJoinColumns = { #JoinColumn(name = "IngredientID") }
)
private Set<MySQLIngredientWithAmountHibernateEntity> ingredience;
public MySQLPizzaHibernateEntity(String name, double smallPrice, double middlePrice, double bigPrice, double familyPrice) {
this.name = name;
this.smallPrice = smallPrice;
this.middlePrice = middlePrice;
this.bigPrice = bigPrice;
this.familyPrice = familyPrice;
}
public MySQLPizzaHibernateEntity() {
}
}
#Entity
#Table(name = "Zutatenliste")
#SecondaryTable(name = "Pizza_Ingredience_Relation", pkJoinColumns = #PrimaryKeyJoinColumn(name = "IngredientID", referencedColumnName = "ID"))
public class MySQLIngredientWithAmountHibernateEntity {
#Id
#Column(name = "ID")
private int id;
#Column(name = "Name")
private String name;
#Column(table = "Pizza_Ingredience_Relation", name="Amount")
private int amount;
#Column(table = "Pizza_Ingredience_Relation", name = "Volume_Unit")
private char unit;
public MySQLIngredientWithAmountHibernateEntity(String name) {
this.name = name;
}
public MySQLIngredientWithAmountHibernateEntity() {
this("");
}
}
I get the following error message:
20:41:45 [main] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] ERROR - Field 'PizzaID' doesn't have a default value
20:41:45 [main] [org.hibernate.internal.ExceptionMapperStandardImpl] ERROR - HHH000346: Error during managed flush [org.hibernate.exception.GenericJDBCException: could not execute statement]
I'm not sure what is wrong in detail I guess it has todo with the PizzaID Foreign Key and that it isn't set properly.
For many to many relationship, you are using middle table with extra columns and you need Embeddable key for that which would comprise of Pizza and Ingredient object (names shortened). Something like:
#Embeddable
public class PizzaIngredientPk {
private MySQLPizzaHibernateEntity pizza;
private MySQLIngredientWithAmountHibernateEntity ingredient;
#ManyToOne
public MySQLPizzaHibernateEntity getPizza() {
return pizza;
}
public void setPizza(MySQLPizzaHibernateEntity pizza) {
this.pizza = pizza;
}
#ManyToOne
public MySQLIngredientWithAmountHibernateEntity getIngredient() {
return ingredient;
}
public void setIngredientID(MySQLIngredientWithAmountHibernateEntity ingredient) {
this.ingredient = ingredient;
}
}
Then this would act as Embedded Key in MySQLIngredientWithAmountHibernateEntity as
#EmbeddedId
PizzaIngredientPk pk = new PizzaIngredientPk();
But this won't work with Secondarytable which is used for one-to-one relationship. #SecondaryTable requires mapping to be with a primary key but in this case Embedded ID would become PK. In fact, you have flaw in your design. You are trying to make one side of your many to many relationship as one-to-one.
As per JPA docs There must be only one EmbeddedId annotation and no Id annotation when the EmbeddedId annotation is used.
Try to specify to Hibernate automatically produce primary keys. Put this annotation above your Id fields and re-create your database.
#GeneratedValue(strategy = GenerationType.IDENTITY)
See: https://thoughts-on-java.org/hibernate-tips-use-auto-incremented-column-primary-key/

Many-to-Many Association with Extra Columns

I am not able to map correctly even following all the steps provided in:
https://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
CREATE TABLE `tb_pla_playlist` (
`id_playlist` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`nm_playlist` VARCHAR(10) NOT NULL,
PRIMARY KEY (`id_playlist`) USING BTREE,
)
CREATE TABLE `tb_mid_midia` (
`id_midia` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`nm_midia` VARCHAR(10) NOT NULL,
PRIMARY KEY (`id_midia`) USING BTREE
)
CREATE TABLE `tb_mip_midia_playlist` (
`id_midia` INT(10) UNSIGNED NOT NULL,
`id_playlist` INT(10) UNSIGNED NOT NULL,
`nr_ordem` INT(10) UNSIGNED NOT NULL,
`CREATED_BY` VARCHAR(10) NOT NULL,
PRIMARY KEY (`id_midia`,`CATEGORY_ID`),
CONSTRAINT `FK_id_midia` FOREIGN KEY (`id_midia`)
REFERENCES `midia` (`id_midia`),
CONSTRAINT `FK_id_playlist` FOREIGN KEY (`id_playlist`)
REFERENCES `playlist` (`id_playlist`)
)
playlist.java
#Entity
#Table(name="tb_pla_playlist")
public class Playlist implements Serializable {
private Set<MidiaPlaylist> midiaPlaylist = new HashSet<MidiaPlaylist>();
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="id_playlist")
private Long idPlaylist;
#OneToMany(mappedBy = "id.playlist", cascade = CascadeType.ALL)
private Set<MidiaPlaylist> getMidiaPlaylist(){
return midiaPlaylist;
}
...
}
[erro: In attribute 'midiaPlaylist', the "mapped by" value 'id.playlist' cannot be resolved to an attribute on the target entity.]
midia.java
#Entity
#Table(name="tb_mid_midia")
public class Midia implements Serializable {
private Set<MidiaPlaylist> midiaPlaylist = new HashSet<MidiaPlaylist>();
#Id
#GeneratedValue
#Column(name="id_midia")
private Long idMidia;
#OneToMany(mappedBy = "id.midia", cascade=CascadeType.ALL)
public Set<MidiaPlaylist> getMidiasPlaylist() {
return midiaPlaylist;
}
[erro: In attribute 'midiasPlaylist', the "mapped by" value 'id.midia' cannot be resolved to an attribute on the target entity.]
MidiaPlaylistPK.java
#Embeddable
public class MidiaPlaylistPK implements Serializable {
private Midia midia;
private Playlist playlist;
#ManyToOne(cascade = CascadeType.ALL)
public Midia getMidia(){
return midia;
}
public void setMidia(Midia midia){
this.midia = midia;
}
#ManyToOne(cascade = CascadeType.ALL)
public Playlist getPlaylist(){
return playlist;
}
public void setPlaylist(Playlist playlist){
this.playlist = playlist;
}
}
MidiaPlaylist.java
#Entity
#Table(name="tb_mip_midia_playlist")
#AssociationOverrides({
#AssociationOverride(name="id.playlist", [erro: Persistent type of override attribute "id.playlist" cannot be resolved]
joinColumns = #JoinColumn(name = "id_playlist")),
#AssociationOverride(name="id.midia", [erro: Persistent type of override attribute "id.midia" cannot be resolved]
joinColumns = #JoinColumn(name = "id_midia"))
})
public class MidiaPlaylist implements Serializable {
private MidiaPlaylistPK id = new MidiaPlaylistPK();
private int nrOrdem;
#EmbeddedId [erro: Embedded ID class should not contain relationship mappings]
public MidiaPlaylistPK getId() {
return this.id;
}
#Column(name="nr_ordem", nullable=false)
public int getNrOrdem(){
return this.nrOrdem;
}
#Transient
public Midia getMidia(){
return getId().getMidia();
}
#Transient
public Playlist getPlaylist(){
return getId().getPlaylist();
}
Relational model
Can someone help me with these errors? Ty!
You should remove #ManyToOne anotation in #Embeddable and use #MapId anotation insted of #Transient, there is an example here:
http://www.objectdb.com/api/java/jpa/MapsId

JPA Hibernate - Mapping MySQL Composite keys to JPA (Hibernate) entities

I am trying to map the following table
CREATE TABLE Person (
p_id varchar(255) not null,
p_name varchar(255 not null,
p_post_code varchar(12) not null,
primary key (p_id, p_name),
);
Usually when i map an Entity to the above table i would do something like this (for single column primary keys):
private int p_id;
private String p_name;
private String p_post_code;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="p_id")
public Long getPId() {
return p_id;
}
public void setPId(int p_id) {
this.p_id = p_id;
}
#Column(name="p_name")
public String getPname() {
return p_name;
}
public void setPname(String string) {
this.p_name = string;
}
#Column(name="p_post_code")
public String getPostCode() {
return p_post_code;
}
public void setPostCode(String string) {
this.p_post_code = string;
}
The above works if the primary key is a single column (i.e. p_id) and the value for this column is generated in the database.
How would i modify the above to map it so that both p_id and p_name are the primary key.
Also, how would this work, if the composite key is a foreign key in another table.
I am trying to google for some examples but i cant find a simple example and most seem to be using the XML based configuration.
When using composite keys with JPA you need to use an embedded class as an id.
In your case you would have a person class and a primary key class for person:
#entity
public class Person
{
#EmbeddedId
private PersonPK key;
#Column(name="p_post_code", nullable = false)
private String p_post_code;
//....
}
#Embeddable
public class PersonPK
{
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="p_id");
private Long p_id;
#Column(name="p_name")
private String p_name;
public PersonPK(String name)
{
p_name = name;
}
//....
}
Using a class for the person's name (so the name is also a foreign key):
#entity
public class Person
{
#EmbeddedId
private PersonPK key;
#MapsId(value="p_name_id")
#ManyToOne
#JoinColumn(name = "p_name_id", referencedColumnName = "id")
private Name p_name;
#Column(name="p_post_code", nullable = false)
private String p_post_code;
//....
}
#Embeddable
public class PersonPK
{
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="p_id");
private Long p_id;
#Column(name="p_name_id")
private Long p_name_id;
public PersonPK(Name name)
{
p_name_id = name.getId();
}
//....
}
#Entity
public class Name
{
#Id
#GeneratedValue(some generation strategy here)
#Column(name="id")
private Long id;
#Column(name="name")
private String name;
//....
}

Categories

Resources