How can I include a referenced table column in a unique constraint? I want to create a unique constraint for table Survey for columns toEmail, receipt.receiptSeries and receipt.receiptNum.
See entities below please:
Receipt
#Entity
#Table(name = "RECEIPT")
public class Receipt {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "RECEIPT_SERIES")
private String receiptSeries;
#Column(name = "RECEIPT_NUM")
private String receiptNum;
}
Survey
#Entity
#Table(name = "SURVEY"}
public class Survey {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#ManyToOne(optional = false)
#JoinColumn(name = "RECEIPT_ID")
private Receipt receipt;
#Column(name = "TO_EMAIL")
private String toEmail;
}
I've tried using the approach below with no success:
#Table(name = "SURVEY", uniqueConstraints = {
#UniqueConstraint(columnNames = {"TO_EMAIL", "RECEIPT.RECEIPT_SERIES", "RECEIPT.RECEIPT_NUM"})})
Using a composite primary key with receiptSeries and receiptNum for Receipt is not an option.
Try something like this
#Table(name="notification_status", uniqueConstraints=
arrayOf(UniqueConstraint(columnNames= arrayOf("external_user_id",
"external_organization_id",
"notification_id"))))
Related
I have a table schema like this. Notification can be of 2 types : twitter or fb
For each notification, there will be 1 row in notification table and 1 in either twitter_post/ fb_post.
Each table have auto sequence id generator.
FBPost and TwitterPost is mapped via notification_id (PK)
I have created entity class like this:
#Entity
#Table(name = "notification")
public class Notification {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "not_id_sequence")
private String id;
#Column(name = "description")
private String description;
#OneToOne(mappedBy = "notification")
private FBPost fbPost;
#OneToOne(mappedBy = "notification")
private TwitterPost twitterPost;
}
#Entity
#Table(name = "fb_post")
public class FBPost {
#Id
#Column(name = "post_id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_fid_sequence")
private Long postId;
#Column(name = "post_content")
private String postContent;
#OneToOne
private Notification notification;
}
#Entity
#Table(name = "twitter_post")
public class TwitterPost {
#Id
#Column(name = "post_id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_tid_sequence")
private Long postId;
#Column(name = "post_content")
private String postContent;
#OneToOne
private Notification notification;
}
I am using JpaRepository to save into the database. But not getting any row into fb_post or twitter_post table.
Am i doing it wrong in entity class.
Try adding #JoinColumn(name = "notification_id", referencedColumnName = "notification_id") below #oneToOne in TwitterPost and FBPost class for the notification variable
Looks like you are missing cascade. Try to add cascade = CascadeType.ALL into #OneToOne of the parent entity - Notification:
#Entity
#Table(name = "notification")
public class Notification {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "not_id_sequence")
private String id;
#Column(name = "description")
private String description;
#OneToOne(mappedBy = "notification", cascade = CascadeType.ALL)
private FBPost fbPost;
#OneToOne(mappedBy = "notification", cascade = CascadeType.ALL)
private TwitterPost twitterPost;
}
For more information I would recommend to read hot to map one-to-one relationship.
I am working on mapping a relationship using a composite key, but the composite key is a separate table.
Car Table
car Id
car description
car Value
SafetyReport Table
safetyReport Id
safetyReport Date
safetyReport Value
CompositeKey CarSafetyReport Table
carid
safetyReportId
One To Many: A car will have many safety reports
JPA:
#Data
#Entity
#Table(name = "CarTable")
public class CarEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "CarId", nullable = false)
private int carId;
#Column(name = "CarDescription", nullable = false)
private String carDescription;
#Column(name = "CarValue", nullable = false)
private String carDescription;
}
#Data
#Entity
#Table(name = "SafetyReport")
public class SafetyReportEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "SafetyReportId", nullable = false)
private int SafetyReportID;
#Column(name = "SafetyReportDate", nullable = false)
private OffsetDataTime date;
#Column(name = "SafetyReportValue", nullable = false)
private String safetyReportValue;
}
#Data
#Entity
#Table(name = "CarSafetyReport")
public class CarSafetyReportEntity implements Serializable {
#EmbeddedId
private CarSafetyReportPk id;
}
#Data
#AllArgsConstructor
#NoArgsConstructor
#Embeddable
public class CarSafetyReportPk implements Serializable {
#Column(name = "CarId", nullable = false)
private int carId;
#Column(name = "SafetyReportId", nullable = false)
private int SafetyReportId;
}
I tried
public class CarEntity {
#OneToMany(mappedBy = "id.carId" )
private List<CarSafetyReportEntity> carSafetyReportEntity;
}
I also tried putting the relationship in the composite key CarSafetyReportPk but i got an error for the annotation #OneToMany in a composite key.
I have main table merchants and second table terminals:
Merchant table:
#Entity
#Table
public class Merchants {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id", updatable = false, nullable = false)
private int id;
#Column
private String name;
#Column
private String login;
}
Terminal table:
#Entity
#Table
public class Terminals {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id", updatable = false, nullable = false)
private int id;
#Column
private int merchant_id;
#Column
private String mode;
}
I want to set merchant_id into table Terminals. I suppose that many to many will be proper relation. How I can create it?
If you have a Join Table:
On Merchants class:
#ManyToMany
#JoinTable(name="MER_TER", joinColumns=#JoinColumn(name="MERCH_ID", referencedColumnName="id"),
inverseJoinColumns=#JoinColumn(name="TERM_ID", referencedColumnName="id"))
private List<Terminals> terminalsList;
On Terminals class:
#ManyToMany(mappedBy="terminalsList")
private List<Merchants> merchantsList;
Page of reference: link
If you don't have a Join Table, try to look here: https://stackoverflow.com/a/25018992
everyone! Sorry but I need some help. My db is looking this way, I try to do same throught the Hibernate in Java.
But I don`t understand how I need to annoted this relations with so many different tables.
It`s a part of my Abiturient table.
#Entity
#Table (name = "abiturient",
uniqueConstraints = {#UniqueConstraint(columnNames = {"id"})})
public class Abiturient {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", nullable = false, unique = true, length = 11)
#ManyToOne
private Long id;
#Column
private Date data_narodjennya;
#Column
private Integer city_village;
It`s a part of my nomer_telefonu table
#Entity
#Table(name = "nomer_telefonu")
public class NomerTelefonu {
#Id
#Column(name = "nomer_tel_id", nullable = false, unique = true, length = 11)
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long nomer_tel_id;
#Column(name = "nomer")
private String nomer;
#Column(name = "id")
#OneToMany(fetch = FetchType.LAZY)
private Set<Abiturient> id;
I don`t think that all there is right, `cos every time I try to solve problem I get an error and need other type.
Use #OneToMany bidirectional relation.
#Entity
public class Abiturient {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToMany(mappedBy = "abiturent")
private List<NomerTelefonu> phones = Lists.newArrayList();
}
#Entity
public class NomerTelefonu{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "nomer_tel_id")
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
private Abiturient abiturent;
}
Lists.newArrayList() from guava. This is working mapping. Always use the simplest version to experiment.
what is #JoinColumn and how it is used in Hibernate
You can use this project to play with mappings in the unit tests:
https://github.com/v-ladynev/hibernate-experimental
I use hibernate and spring-data. There are two tables with many-to-many relationship.
#Entity
#Table(name = "FirstEntity")
public class FirstEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "first_entity_id")
private Long id;
#Column(name = "first_entiry_name")
private String name;
/* getters and setters are below*/
}
#Entity
#Table(name = "SecondEntity")
public class SecondEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "second_entity_id")
private Long id;
#Column(name = "second_entiry_name")
private String name;
#Column(name = "second_entiry_desc")
private String description;
/* getters and setters are below*/
}
And entity for cross-reference table.
#Entity
#Table(name = "FirstSecondEntity")
public class FirstSecondEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "first_second_entity_id")
private Long id;
#Column(name = "first_entity_id")
private Long firstEntityId;
#Column(name = "second_entity_id")
private Long secondEntityId;
/* getters and setters are below*/
}
I need SELECT like this
SELECT FirstEntity.name, SecondEntity.name, SecondEntity.description FROM SecondEntity INNER JOIN FirstSecondEntity ON SecondEntity.id = FirstSecondEntity.secondEntityId INNER JOIN User ON FirstEntity.id = FirstSecondEntity.firstEntityId
i.e. I need all records from cross-reference table where instead of ids there is actual info from entities.
Inserting this query into #Query annotation in my CrudRepository-extended class doesn't work because of
ERROR [main][org.hibernate.hql.internal.ast.ErrorCounter] Path expected for join!
So I need your help.
Your join table is all screwed up. In this case, you actually don't even need the join table as a hibernate mapping:
In Second Entity add the following list:
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "FirstSecondEntity",
joinColumns = {
#JoinColumn(name = "first_entity_id",
nullable = false,
updatable = false) },
inverseJoinColumns = {
#JoinColumn(name = "second_entity_id",
nullable = false,
updatable = false) },
)
private List<FirstEntity> firstEntities;
In FirstEntity add the following list:
#ManyToMany(fetch = FetchType.LAZY,
mappedBy = "firstEntities")
private List<SecondEntity> secondEntities;