I have the following class:
#Entity
public class User {
#Embeddable
public static class Key implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
#Temporal(TemporalType.TIMESTAMP)
private Date effectiveDate;
#Column(unique = true)
private String name;
// Getters, Setters
}
#EmbeddedId
private Key key;
#Column(nullable = false)
private String password;
// Getters, Setters ...
}
I want the id field within key to be generated, unfortunately I can't find a way to do so. I've looked at similar questions here but I can't find the answer (all the solution proposed did not work in this case).
Related
I am currently working on a project with following data structure:
public class PinaColadaId implements Serializable {
private UUID id;
#Temporal(TemporalType.TIMESTAMP)
private Date time;
// Constructor + Getters and Setters
}
#Entity
#Table(name = "pina_coladas")
#IdClass(PinaColadaId.class)
public class PinaColadaEntity {
#Column(columnDefinition = "char(255)")
#Type(type="org.hibernate.type.UUIDCharType")
private #Id UUID id;
private #Id #Temporal(TemporalType.TIMESTAMP) Date time;
#OneToOne(mappedBy = "entityID", cascade=CascadeType.ALL)
private PineappleWrapper pineapple;
#OneToMany(mappedBy = "entityID", cascade=CascadeType.ALL)
private List<CoconutWrapper> coconuts;
// Constructor + Getters and Setters
}
#Entity
public class PineappleWrapper {
private #Id #GeneratedValue long id;
private String manufacturer;
private String origin;
#OneToOne
private PinaColadaEntity entityID;
// Constructor + Getters and Setters
}
#Entity
public class CoconutWrapper {
private #Id #GeneratedValue long id;
private int shipmentNumber;
private int juice;
#ManyToOne
private PinaColadaEntity entityID;
// Constructor + Getters and Setters
}
The issue is that Spring Boot together with Hibernate and JPA correctly generate all the tables in my database, however when I attempt to store PineappleWrapper or CoconutWrapper, it stores all the values of PineappleWrapper and/or CoconutWrapper except for the id and time of the parent. The columns are generated yet they store the value "null".
Any and all help is much appreciated, -AwesomeDude091
Edit: I am aware of the JoinColumn annotation and it proposed implementation in my Wrapper classes, but I do not know how to use them with my two ID variables (id and time)
Those are my classes:
#Entity
#Table(name="assessment")
public class AssesmentProperties {
#Id
#Column(name="AssessmentId")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long AssessmentId;
#Column(unique=true,nullable=false)
private String AssessmentName;
private String AssessmentLevel;
private String Specialization;
private int time;
private String keywords;
private int NoOfSections;
//getters and setters
}
#Embeddable
public class SettingsPrimary implements Serializable {
private Long AssessmentId;
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long Section;
//getters and setters
}
#Entity
#Table(name="section")
public class SectionProperties {
#EmbeddedId
private SettingsPrimary PrimaryKey;
private String SectionType;
private int Weightage;
private int time;
private int NoOfQuestions;
//getters and setters
}
In the table section I need to create assessment_id as FK to assessment table and set cascade on delete. I have tried to do it with different ways but without success.
Maybe this could help you.
#Entity
#Table(name="section")
public class SectionProperties {
#Id
private Long PrimaryKey;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "assessment_id", referencedColumnName="AssessmentId")
private AssesmentProperties AssesmentProperties;
private String SectionType;
private int Weightage;
private int time;
private int NoOfQuestions;
//getters and setters
}
I changed the SectionProperties id to a Long and mapped the AssessmentProprties into a ManytoOne prop.
This way, always that a AssessmentProperties binded to a Section will be deleted, the associated SectionProperties will too.
I need to find an object using two fields of an embedded key
Here is the embedded key:
public class OrderItemId implements Serializable {
private static final long serialVersionUID = 1163347452811191867L;
#Column(name = "order_code", length = 25)
private String orderCode;
#Column(name = "barcode", length = 25)
private String barcode;
// ....
}
Here is the class of the object I want to query:
#Entity
#Table(name = "order_item")
public class OrderItem {
#EmbeddedId
#NotNull
private OrderItemId id;
#Column(name = "quantity")
private Integer quantity;
#Column(name = "price")
private Double price;
// ...
}
As in this StackOverflow Answer
To query by embedded key orderCode, I can write something like this
public List<OrderItem> findById_OrderCode(String orderCode);
and it works!
But I don't know how to query by both orderCode and barcode. I have tried some forms of and but no use.
Never mind, I have figured out the query, it is
public OrderItem findById_OrderCodeAndId_Barcode(String orderCode, String barcode);
I am new to hibernate and I am trying to implement a basic application that uses this schema (it does not follow the notation I just use it for clarity)
Here is the my classes
#Entity
#Table(name = "race")
public class Race {
#Id
#GeneratedValue
private UUID id;
private String name;
}
#Entity
#Table(name="np_character")
public class NPCharacter {
#Id
#GeneratedValue
private UUID id;
#OneToOne
private Race race;
private String name;
private int age;
}
#Entity
#Table(name="main_female_character")
public class MainFemaleCharacter {
#Id
#GeneratedValue
private UUID id;
#OneToOne
private Race race;
private String name;
private int age;
}
#Entity
#Table(name="copulation_registry")
public class CopulationRegistry {
// ??
private NPCharacter npCharacter;
// ??
private MainFemaleCharacter femCharacter;
private int times;
}
But I ran into the problem with copulation_registry class. I used everywhere OneToOne annotation, instead of using references to keys. But what I should do here? Pairs of id_femPlayer and id_npCharacter are unique.
Should I use EmbeddedId annotation or is it possible somehow to use association annotations to represent the same relation?
You can annotate class CopulationRegistry with #IdClass
#Entity
#IdClass(CopulationRegistryKey.class)
#Table(name="copulation_registry")
public class CopulationRegistry {
#Id
private NPCharacter npCharacter;
#Id
private MainFemaleCharacter femCharacter;
private int times;
}
public class CopulationRegistryKey{
private NPCharacter npCharacter;
private MainFemaleCharacter femCharacter;
}
i'm trying to create tabel entity with composite primary key.
so i tried to create #Embeddable class with all the primary keys and use #EmbeddedId on the field that represents the primary keys. but i get this error:
Caused by: org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
i wos tried to follow this link bat noting work
what i'm doing wrong?
here is my code
DocEntity
#Entity(name = "doc")
#Table(name = "doc")
public class DocEntity extends baseDataBase implements Serializable
{
private static final long serialVersionUID = 1L;
#EmbeddedId
private DocID docID;
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "DocID")
#JsonProperty
#NotEmpty
private short id;
#Column(name = "DocDescription")
#JsonProperty
#NotEmpty
private String docDescription;
#Column(name = "DocPath")
#JsonProperty
#NotEmpty
private String docPath;
// getters, setters ,equals, hashCode
}
DocID
#Embeddable
public class DocID implements Serializable
{
private static final long serialVersionUID = 1L;
#Column(name = "DocName")
#JsonProperty
#NotEmpty
protected String docName;
#Column(name = "DocVersion")
#JsonProperty
#NotEmpty
protected String docVersion;
// getters, setters ,equals, hashCode
}
what i'm doing wrong?