Composite Key with subclass in hibernate - java

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

Related

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

composite key for the table using association annotation

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

One to One Weak Entity Mapping JPA

I have a Entity Class
#Entity
Class Search
{
#Id
private Long SearchID;
private String Type;
}
and another Entity Class ( SearchResults which is a weak Entity that depends on Search Class for its Primary Key
#Entity
Class SearchResults
{
#???
private Long SearchID;
}
What annotation should i use to assign "SearchID" of "Search" Entity class as my primary key in my weak Entity " SearchResults"
Using the JPA concept of Shared Primary Key, you can map your relationship as follows:
Your main class:
#Entity
public class Search {
#Id
private Long searchID;
private String type;
}
Derived Identifier with Single Attribute
#Entity
public class SearchResults {
#Id
#OneToOne
#JoinColumn(name = "SEARCHID")
private Search search;
}
Deriver Identifier with Shared Mappings
#Entity
public class SearchResults {
#Id
private Long searchID;
#MapsId
#OneToOne
#JoinColumn(name = "SEARCHID")
private Search search;
}
Full article here: http://vard-lokkur.blogspot.com.br/2014/05/onetoone-with-shared-primary-key.html
SearchResult does not have to be an entity.
While it can be mapped as an Enity with a shared PK as suggested in the other answer, as a Weak entity it cannot exist independently of its associated Search and so can be mapped as an Embeddable.
https://en.wikibooks.org/wiki/Java_Persistence/Embeddables
#Entity
public class Search
{
#Id
private Long SearchID;
private String Type;
#ElementCollection
#CollectionTable(....)
private Set<SearchResults> results;
}
#Embeddable
public class SearchResults
{
//does not need an Id
//other fields
}

How to inherit #Id from mapped superclass?

I want to create a primary composite key and use an #Id field from a parent class. But it does not work. Why?
#MappedSuperclass
static abstract class SuperEntity {
#Id
private Long id;
}
#Entity
#IdClass(SuperPK.class)
public static class ChildEntity extends SuperEntity {
#Id
private String lang;
}
public class SuperPK {
public SuperPK(Long id, String lang) {
//...
}
}
Result: Property of #IdClass not found in entity ChildEntity: id
I found an open issue regarding this bug.
One of the comments states to override the getters for the ID properties as a workaround.
#Entity
#IdClass(SuperPK.class)
public static class ChildEntity extends SuperEntity {
#Id
private String lang;
#Override #Id
public Long getId() {
return super.getId();
}
}

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