composite key for the table using association annotation - java

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;
}

Related

Add foreign key using hibernate and jpa

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.

Will the findAll function of Spring Data JPA repository work if the entites are related as many to one or one to one or any such association?

Here is how the entity class looks like
#Entity
public class IndustryCode {
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String industryName;
#OneToMany(mappedBy="industryCode")
private Set<CarrierCodes> industryCodes;
#Entity
public class TechCode {
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String techName;
#OneToMany(mappedBy="techCode")
private Set<CarrierCodes> techCodes;
#Entity
public class CarrierCodes {
#EmbeddedId
private CarrierCodesId id = new CarrierCodesId();
#ManyToOne
#MapsId("techCodeId")
private TechCode techCode;
#ManyToOne
#MapsId("industryCodeId")
private IndustryCode industryCode;
#SuppressWarnings("serial")
#Embeddable
public class CarrierCodesId implements Serializable {
private Long industryCodeId;
private Long techCodeId;
#Entity
public class Register {
#Id
private Long mobileNumber;
#ManyToOne
// optional but nice to have consistent names
#JoinColumns({
#JoinColumn(name="industryCode_id", referencedColumnName="industryCode_id"),
#JoinColumn(name="techCode_id", referencedColumnName="techCode_id")
)
private CarrierCodes carrierCodes;
}
public class RegisterRepository extends JPARepository<Register,mobileNumber>{
}
My question is if I run findAll on Register table will I get the data for other related tables as well?
I mean using findAll(), will I get a List from which I can take Register obj and use .getCarrierCode().getIndustryCode().getIndustryName() to get industry name corresponding to carrierCode value in Register table

java - Hibernate Search - Unable to perform work. Entity Class is not #Indexed nor hosts #ContainedIn

I have a Spring JPA project with 3 entities: Author, Book and Category.
I want to use Hibernate Search for indexes.
Author class is #Indexed; Book class contains a Category field annotated with #ContainedIn; Category is a very simple class.
CLASS Author
#Entity
#Table
#Indexed
public class Author extends ConcreteEntity {
private static final long serialVersionUID = 1L;
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#IndexedEmbedded
private List<Book> books = new ArrayList<>();
}
CLASS Book
#Entity
#Table
public class Book extends ConcreteEntity {
private static final long serialVersionUID = 1L;
#ContainedIn
private Category category;
}
CLASS Category
#Entity
#Table
public class Category extends ConceptEntity {
private static final long serialVersionUID = 1L;
}
CLASS ConcreteEntity and ConceptEntity are similars:
#MappedSuperclass
public abstract class ConcreteEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String name;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String value;
}
#MappedSuperclass
public abstract class ConceptEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String name;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String value;
}
I've got this exception while saving a resource using Hibernate Search.
org.hibernate.search.exception.SearchException: Unable to perform work. Entity Class is not #Indexed nor hosts #ContainedIn: class test.hibernate.search.Category
I don't understand how to solve this issue.
Thanks
Book is not configured correctly. You tell Hibernate Search that Book is included in the Category index (via your #ContainedIn annotation on the category field) but your Category entity is neither marked with #Indexed nor linked to another index via #ContainedIn.
Hibernate Search is just telling you that your configuration doesn't make much sense.
Considering your model, I'm pretty sure you wanted to mark category with #IndexedEmbedded instead.

Composite Key with subclass in hibernate

I have a Class system
#Entity
abstract class System{
#Id
int systemId;
//setter and getters..
}
and which is extended by class
#Entity
class PhysicalSystem extends System
{
#Id
int place;
//setter and getters..
}
in need to make the composite key by using the systemId and place
how can i do this.. if i have #Id in both class its throws exception
Initial SessionFactory creation failed.java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass cannot be cast to org.hibernate.mapping.RootClass
How can i solve this?
Tables:
System{
systemid PK
systemName
}
PhysicalSystem
{
systemId PK
locationId PK
}
In your case, maybe the best solution is a OneToOne mapping:
#Entity
public class PhysicalSystem implements Serializable {
#EmbeddedId
private PhysicalSystemKey key;
#JoinColumns({JoinColumn(name = "key.systemId", referencedColumnName = "ctvId"})
#OneToOne(mappedBy = "physicalSystem")
private System system;
// ...
}
#Embeddable
public class PhysicalSystemKey {
private Long systemId;
private Long locationId;
// ...
}
#Entity
public class System implements Serializable {
#Id
private Long systemId;
#OneToOne(mappedBy = "system")
private PhysicalSystem physicalSystem;
}

How to make an #ManyToOne entity the Id of a class in hibernate

I have a problem and tried already allot of ways to solve it.
the problem is quite simple how can I use both the Item ID and the amount as a primary key?
Since this will make the item a tiny blob. And if I use #MapsId it gives the exact same thing.
#Entity
public class C_Drop extends LightEntity implements Serializable {
#Id
#ManyToOne
private C_Item item;
#Id
private double amount;
}
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class C_Drops extends LightEntity implements Serializable {
#Id
double id;
#OneToMany
private List<C_Drop> drops;
}
I haven't done this myself, but you could probably use a combination of a Composite Primary Key and double-mapping the column. e.g.:
#Entity
#IdClass(C_DropPK.class)
public class C_Drop extends LightEntity {
#Id
private double amount;
#Id
#Column(name = "ITEM_ID")
private double itemId;
#ManyToOne
#JoinColumn(name="ITEM_ID")
private C_Item item;
}
Then:
public class C_DropPK {
private double itemId;
private double amount;
}

Categories