Mpping each enum type to each columns using jpa entity - jpa

This is my Entity.
#Entity
public class Student{
...
#Column
private boolean english;
#Column
private boolean math;
...subject
}
I want to manage subect using enum.
so, I create it.
enum Subject{
Math,
English,
...
}
And I want to use the enum class in the Entity.
So I search jpa enum.
Most example use #enumerated.
It is
subject
--------
math
english
but i want to
math | english
-------------
true | false
this.
how to fill blank?
#Entity
public class Student{
...
/* blank */
}

Related

JPA, Map entity Person which contains class of type Name

I have an entity Person which consists of a name Attribute
#Entity
public class Person {
// ...
//#Transient
private Name name;
// ...
}
I dont want to store "name" in an extra table... I mark name as transient so it is not stored in the underlying database.
What I want is to map the attribute "name" to columns "first_name" and "last_name" in the database.
For example I can create a person like new Person(new Name("John","Doe"));
How can I achieve a mapping that the underlying table contains two additional columns first_name and last_name and the contents are the strings which I get from the name attribute?
The table, based on the person entity should look like
id|first_name|last_name
1 |John | Doe
2 |Jane | Doe
You can use embeddables:
#Embeddable
public class Name {
private String firstName;
private String lastName;
// getters and setters
}
And then use it like.
#Entity
public class Person {
// ...
private Name name;
// ...
}
Read more about embeddables in the Hibernate documentation: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#embeddables

JPA #ManyToOne with different datatypes

I have two old database tables, that i need to use with JPA.
TOUR VEHICLE
----------------------- ---------------------
Id NUMBER(10) VehicleNumber CHAR(3)
VehicleNumber NUMBER(3) LicensePlate CHAR(10)
In my JPA entities I want tu use a #ManyToOne relationship from TOUR to VEHICLE.
Vehicle Entity:
public class Vehicle {
#Id
#Column(length=3)
private String VehicleNumber;
...
Tour Entity:
public class Tour {
#Id
#Column(precision=3)
private BigDecimal Id;
#ManyToOne(fetch=FetchType.EAGER)
#JoinColumn(name="VehicleNumber", referencedColumn="VehicleNumber")
private Vehicle vehicle;
...
But this of course doesn't work since the one vehicle number is translatet to String and the other is translatet to a BigDecimal(precision=3).
So what can i do to join them? Non-numeric values should be ignored.
Thanks for any advice.
VehicleNumber in TOUR table is a number while in the other one is CHAR.
First you need to match the types (better not to try to hack the standards).
then for #Id put the column name not #Column(precision=3). so it should be:
public class Tour {
#Id
#Column(name = "Id")
private BigDecimal id;
...
}
and do the same for the other table.
Oh by the way, I suggest you to use Long for the Primary Key not BigDecimal unless you have to.
But for entities, you are ok to work with Long

JPA using `Enum` as an `#Id` with `#IdClass`

I found a forum with this subject. Using enum as id
But I couldn't sure that I can do it or not. I think my case is a little bit different.
enum Type {
}
class MyEntityId {
String type;
String key;
}
#Entity
#IdClass(MyEntityId.class)
class MyEntity {
#Enumerated(EnumType.String)
#Id
Type type;
#Id
String key;
}
Is this actually legal by the spec?
If it is, can I have no worries for vendor specific behaviors?
Why not read the linked issue ? It quotes the spec (2.1.4) and says NO you cannot have id fields of Enum type; it doesn't matter that you have some (invalid) IdClass (since the types of an IdClass have to match the types of the class). So it is not part of the JPA spec, so you are left with vendor-specific behaviour.
In Hibernate 4 you can use an Enum as an #Id in an IdClass, but only as an Ordinal. Any #Convert tags you add the the enumeration will be ignored.
What you can do is use an #EmbeddedId to get the same effect
#Embeddable
class MyEntityId {
#Enumerated(EnumType.String)
Type type;
String key;
}
#Entity
class MyEntity {
#EmbeddedId
MyEntityId id;
}

JPA 2.0 map one class to different columns of one table

i have Entity mapped to table TABLE1 with columns COLUMN1 and COLUMN2
i have class ResViewer
public class ResViewer() {
private boolean flag;
private int property;
...selectors
}
i have entity class
#Entity
#Table(name="TABLE1")
public class Table1() {
#Id
private long id;
private ResViewer res1;
private ResViewer res2;
...selectors
}
How can i map field flag of classes res1 and res2 to columnds COLUMN1 and COLUMN2?
Your ResViewer needs to be annotated with #Embeddable, and the fields res1 and res2 must be annotated with #Embedded, and with #AttributeOverrides, as demonstrated in the javadoc of #Embedded.

jpa Using WHERE clause

I have 2 entities:
#Entity
public class Elements implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Owner owner;
}
#Entity
public class Owner implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(fetch=FetchType.LAZY)
List<Elements> elements;
}
Suppose I want to fetch all elemets bellonging to the owner from Elements Table and therfore I use:
TypedQuery query=em.createQuery("SELECT elem FROM Elements elem WHERE
elem.owner:=elemOwner", Elements.class);
query.setParameter("elemOwner", ownerObjectFetchFromDataBase);
List<TrendUsers> userList=query.getResultList();
But I get the following error:
Comparisons between 'BLOB' and 'BLOB' are not supported. Types must be comparable. String types must also have matching collation.
If collation does not match, a possible solution is to cast operands to force them to the default collation...
Is there any way I can Select from Elements Table and in the WHERE clause use object (and not just String,int...)?
(p.s I also tried the query below and it didn't work:
TypedQuery query=em.createQuery("SELECT elem FROM Elements elem WHERE elem.owner.id:=elemOwner", Elements.class);
query.setParameter("elemOwner", ownerObjectFetchFromDataBase.id);
List userList=query.getResultList();
)
Thanks
You need to mark the Owner als a ManyToOne.
#Entity public class Elements implements Serializable {
...snip ...
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="OWNER_ID")
private Owner owner;
}
#Entity public class Owner implements Serializable {
.. snip ...
#OneToMany(fetch=FetchType.LAZY, mappedBy="owner")
List<Elements> elements;
}
Right now you try to store the serialized owner in Blob. Thats not what you want ;-)
enjoy
Edit: included fix by xatavt