Now, I'm aware that an Inner class cannot be an Entity in Hibernate.
I'll first show my code, please refer to my question below:
#Entity
#Table(name = "bags")
public class Bags extends AbstractModel {
private String brand;
private String condition;
private String size;
private Extras extras;
#ManyToOne
private Customer customer;
private class Extras {
private boolean box;
private boolean authenticity_card;
private boolean shoulder_strap;
private boolean dustbag;
private boolean pouch;
private boolean padlock_and_key;
private boolean bagcharm;
private boolean nameTag;
private boolean mirror;
}
}
Getters and setters are ommited. My question is:
If I want to have a slightly more complex object such as Extras, in which I represent the absence or not of several accessories, would it be better to create an additional table associated with bags OR is there a way around this?
Please let me know if I was not clear or you require additional information.
#Embeddable annotation is used to declare a class will be embedded by other entities.
#Embeddable
public class Extras {
private boolean box;
private boolean authenticity_card;
private boolean shoulder_strap;
private boolean dustbag;
private boolean pouch;
private boolean padlock_and_key;
private boolean bagcharm;
private boolean nameTag;
private boolean mirror;
}
#Embedded is used to embed a type into another entity.
#Entity
#Table(name = "bags")
public class Bags extends AbstractModel {
private String brand;
private String condition;
private String size;
private Extras extras;
#ManyToOne
private Customer customer;
#Embedded
private Extras extras;
}
Related
I am trying to create a search engine that will show results containing different tags. The results must be sorted by relevance.
Lets say I have the following models:
public class Article extends RealmObject{
#PrimaryKey
private String aID = UUID.randomUUID().toString();
private RealmList<Tags> tags;
}
public class Tags extends RealmObject{
#PrimaryKey
private String tID = UUID.randomUUID().toString();
private String tag;
private RealmList<articleTagsRelation> articlesTag;
}
public class articleTagsRelation extends RealmObject{
private String tID;
private String aID;
private long timesArticleSelectedByTag;
}
So the relation between RealmObjects are:
Article (many-to-many) tags (many-to-many) articleTagsRelation
When users search by tag, the app should return all the Articles that fill the search:
realm.where(Article.class).equalTo("tags.tag", userSearch).findAll().
However, I would like to sort the results by relevance. Relevance in this example is the number of times users have selected the article when searching by the related tag (articleTagsRelation.timesArticleSelectedByTag).
I have been struggling for a while to find a direct way to accomplish this with no result. Is there any direct way to do it? if not, sorting the results one by one will be the unique solution?
Thanks
Realm 3.5.0+:
public class Article extends RealmObject{
#PrimaryKey
private String aID = UUID.randomUUID().toString();
private RealmList<Tags> tags;
#LinkingObjects("article")
private final RealmResults<ArticleTagsRelation> articleOfRelation = null;
}
public class Tags extends RealmObject{
#PrimaryKey
private String tID = UUID.randomUUID().toString();
private String tag;
#LinkingObjects("tag")
private final RealmResults<ArticleTagsRelation> tagOfRelation = null;
}
public class ArticleTagsRelation extends RealmObject{
private Tags tag;
private Article article;
private long timesArticleSelectedByTag;
}
realm.where(Article.class)
.equalTo("tags.tag", userSearch)
.findAllSorted("articleOfRelation.timesArticleSelectedByTag", Sort.DESCENDING);
I want to insert doctor object to database, how should I put annotations for properties?
I tried to do it with te code shown below.
But i don't know how to do it on list properties specializations and phoneNumbers.
#Table(databaseName = WMDatabase.NAME)
public class Doctor extends BaseModel{
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Column
private FullName fullName;
#Column
private String organizationId;
#Column What shuld i put here?????
private List<Specialization> specializations;
#Column What shuld i put here?????
private Contacts contacts;
}
Below are the classes I use for doctor attributes:
public class Contacts extends BaseModel {
private List<PhoneNumber> phoneNumbers;
private String email;
private String fax;
}
public class Specialization extends BaseModel {
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Unique(unique = true)
private String specializationName;
public String getSpecializationName() {
return specializationName;
}
public void setSpecializationName(String specializationName) {
this.specializationName = specializationName;
}
DBFlow is a relational database system (not a mongo-type key/value store) and doesn't support lists as columns, according to the doc here.
List : List columns are not supported and not generally proper for a relational database. However, you can get away with a non-generic List column via a TypeConverter. But again, avoid this if you can.
The documentation on relationships may help you refine the model to suit your needs.
I'm not able to get the total count of my query.
JPAQuery query = super.prepareJPAQuery(userAccountHasWorkgroup).where(
userAccountHasWorkgroup.workgroup.id.eq(workgroupId);
query.count();
userAccountHasWorkgrouphas an #Embeddable class as ID.
javax.ejb.EJBTransactionRolledbackException: org.hibernate.exception.DataException: Operand should contain 1 column(s)
I add more information:
#Entity
#Table(name = "UserAccount_has_Workgroup")
public class UserAccountHasWorkgroup implements java.io.Serializable {
private static final long serialVersionUID = 6537213525312531347L;
private UserAccountHasWorkgroupId id;
private UserAccount userAccountByUserAccountId;
private Privilege privilege;
private UserAccount userAccountByApprovedByUserAccountId;
private Workgroup workgroup;
private boolean approved;
private boolean lastActiveWorkgroup;
private boolean isWorkgroupReferent;
...
}
#Embeddable
public class UserAccountHasWorkgroupId implements java.io.Serializable {
private static final long serialVersionUID = 6368469866573301127L;
private long userAccountId;
private long workgroupId;
...
}
If i do:
List<UserAccountHasWorkgroup> result = query.list(userAccountHasWorkgroup);
it works, but when I try to count:
Long count = query.count();
I receive the DataException
Instead of:
query.count();
I do
query.uniqueResult(Wildcard.count.as("count"));
It seems to work properly.
I have a question about Moo (https://github.com/geoffreywiseman/Moo/) that I haven't been able to solve on my own. I have this class structure:
class Middle{
private int id;
private Upper upper;
private List<Child> children;
private List<Middle> brothers;
}
class Upper{
private int id;
private String name;
private String lastname;
}
class Child{
private int id;
private String name;
}
and I want to translate them to:
class OutputMiddle{
private int id;
#Property(translation="Upper")
private OutputUpper outputUpper;
#CollectionProperty(itemTranslation = Upper.class)
private List<OutputChild> outputChildren;
private List<OutputMiddle> outputBrothers;
}
class OutputUpper{
private int id;
private String outputName;
}
class OutputChild{
private int id;
private String outputName;
}
What I don't know is:
Am I able to translate one attribute from one class to another attribute of another class?
And do the same but to a collection of objects?
And the same but to a collection of objects of the same class?
Why do I need this? Because I'm returning objects of the class "Middle" as JSON (or XML) and I need it to have an structure such as the "OutputMiddle", but I don't want to intervene the JSON after its creation and change the names of the nodes manually.
Thanks!
Try JMapper Framework, it's ease to use and require few configuration
I am new to hibernate and having a tough time trying to wrap my head around setting up Joined inheritance with composite Primary Key. With my current setup, I get a:
JDBCException: could not insert: LandHolidayPackage
I am essentially looking for two things:
Are the inheritance annotations in place ?
Is the composite PK setup properly ?
DB Design:
Reference
Here are my classes and the annotations involved:
#Entity
#Table(name = "HOLIDAYPACKAGE")
public final class HolidayPackage {
private Integer idPackage;
private String name;
private Set<HolidayPackageVariant> holidayPackageVariants = new HashSet<HolidayPackageVariant>(0);
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "IDHOLIDAYPACKAGE", nullable = false)
public Integer getIdPackage() {
return idPackage;
}
#OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.ALL}, mappedBy = "holidayPackage")
public Set<HolidayPackageVariant> getHolidayPackageVariants() {
return holidayPackageVariants;
}
// ommitted other part of the code
}
#Entity
#Inheritance(strategy=InheritanceType.JOINED)
#Table(name="HOLIDAYPACKAGEVARIANT")
public abstract class HolidayPackageVariant {
private Integer idHolidayPackageVariant;
private HolidayPackage holidayPackage;
private String typeHolidayPackage;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="IDHOLIDAYPACKAGEVARIANT", nullable=false)
public Integer getIdHolidayPackageVariant() {
return idHolidayPackageVariant;
}
#ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
#JoinColumn(name="IDHOLIDAYPACKAGE", nullable=false)
public HolidayPackage getHolidayPackage() {
return holidayPackage;
}
#Column(name="TYPEHOLIDAYPACKAGE", nullable=true)
public String getTypeHolidayPackage() {
return typeHolidayPackage;
}
// ommitted setters, equals hashCode
}
#Entity
#Table(name="LANDHOLIDAYPACKAGEVARIANT")
public final class LandHolidayPackageVariant extends HolidayPackageVariant{
private static final String LAND = "LAND";
protected LandHolidayPackageVariant() {}
public LandHolidayPackageVariant(HolidayPackage holidayPackage) {
super(holidayPackage, LAND);
}
}
#Entity
#Table(name="FLIGHTHOLIDAYPACKAGEVARIANT")
public final class FlightHolidayPackageVariant extends HolidayPackageVariant{
private static final String FLIGHT = "FLIGHT";
private Destination originCity;
protected FlightHolidayPackageVariant(){}
public FlightHolidayPackageVariant(HolidayPackage holidayPackage,
Destination originCity) {
super(holidayPackage, FLIGHT);
setOriginCity(originCity);
}
#ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
#JoinColumn(name="IDDESTINATION", nullable=false)
public Destination getOriginCity() {
return originCity;
}
// ommited other setters etc functions
}
You annotated the properties in stead of the fields. JPA by default tries to access the fields. If you want JPA to use the fields you have to annotate the class with #AccessType(AccessType.Field).