I have a very simple model class.
#Entity
#Table(name="reach")
public class Reach {
#Id
#Column(name = "uid")
private Long uId;
#Column(name = "reach_30")
private Integer reach30;
... getters, setters..
}
And here is my meta model
#StaticMetamodel(Reach.class)
public class Reach_ {
public static volatile SingularAttribute<Reach, Long> uId;
public static volatile SingularAttribute<Reach, Integer> reach30;
}
And when i print following
System.out.println("==============="+(Reach_.uId));
System.out.println("==============="+(Reach_.reach30));
I get object value for uId BUT NULL for reach30. Any idea whats going on here.
Thankx
Problem was with eclipse. Needed to clean and restart computer to get it working. But not a problem with code
Related
I found similar questions, but they did not answer my question.
I have two entities with a many-to-one relationship - unidirectional.
But most importantly, the relationship is lazy. Because it is correct to use a lazy connection, everyone knows it.
Code:
#Entity
public class User implements BaseEntity {
#Id
#Column
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column
private String name;
#ManyToOne(fetch = FetchType.LAZY)
private City city;
}
#Entity
public class City implements BaseEntity {
#Id
#Column
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column
private String name;
}
interface BaseEntity {
void setId(Long id);
Long getId();
}
I wrote a method that allows you to search by the transferred fields of the entity.
An example of how this works:
public class Search<T extends BaseEntity> {
public List<T> getByFields(T entity, List<FieldHolder> data) {
// create criteria with passed field name and value by reflection
}
}
class FieldHolder {
private String fieldName;
private Object value;
/**
* "true" - means that the field "value" contains id related object
* "false" - elementary type like: String, Wrapper, Primitive
*/
private boolean isRelationId;
}
The problem is that problems start when you need to search and related objects - by creating related queries.
The following entry is used to send the associated field: "city.id" and the problem is that when I transfer the essence of the related object (City) it is in a proxy and I cannot get id by reflection from City.
My function works perfectly if you specify:
#ManyToOne(fetch = FetchType.EAGER)
private City city;
But it will greatly affect performance, since I have a lot of related objects. Therefore, I want to solve this problem for a lazy load.
I know that this is not an easy task. But perhaps there is some opportunity to somehow get around this problem.
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.
So, after several attempts of trying and trying to make this work the way I want, and of course checking different guide, I now come to you guys.
My program is designed to work like this:
persona (the father object)
-persona_cuil (pk on DB, generated by user)
empleado (persona's son)
-legajo_id (pk on DB, generated by program NOT DB (couldnt make that work either))
-persona_cuil (FK from persona)
empvarios (empleado's son)
-legajo_id (PK and FK from empleado)
Now, the database is mapped that way, and it works just fine, the problem seems to be that hibernate somewhere mixes the primary keys sent to each object, and instead of inserting a legajo_id in empvarios, it inserts a persona_cuil.
Code for clases:
persona:
#Entity
#Table(name = "persona")
#Inheritance(strategy = InheritanceType.JOINED)
public class persona implements Serializable {
private static final long serialVersionUID = 2847733720742959767L;
#Id
#Column(name="persona_cuil")
private String persona_cuil;
#Column(name="nombre")
private String nombre;
#Column(name="apellido")
private String apellido;
#Column(name="fecha_nac")
private String fecha_nac;
#Column(name="direccion")
private String direccion;
#Column(name="localidad")
private String localidad;
#Column(name="provincia")
private String provincia;
#Column(name="pais")
private String pais;
#Column(name="fecha_muerte")
private String fecha_muerte;
#Column(name="fecha_alta")
private String fecha_alta;
#Column(name="fecha_baja")
private String fecha_baja;
#Column(name="mail")
private String mail;
#Column(name="plan_id")
private int plan_id;
public persona (){
this.setPlan_id(0);
}
//Getters and Setters
}
empleado:
#Entity
#Table(name = "empleado")
#Inheritance(strategy = InheritanceType.JOINED)
#PrimaryKeyJoinColumn(name="persona_cuil")
public class empleado extends persona implements Serializable {
private static final long serialVersionUID = -7792000781951823557L;
#Column(name="legajo_id")
private int legajo_id;
public empleado(){
super();
int leg = SentenceManager.ultimoRegistro("empleado");
if (leg == 0){ //this works fine, it just searches the last registry, if it exists, i uses the next available number
this.setLegajo_id(1);
}
else {
this.setLegajo_id(leg+1);
}
}
//Getters and Setters
}
empvarios:
#Entity
#Table(name="empvarios")
#PrimaryKeyJoinColumn(name="legajo_id")
public class empvarios extends empleado implements Serializable, ToPersona{
private static final long serialVersionUID = -6327388765162454657L;
#Column(name="ocupacion_id")
int ocupacion_id;
public empvarios() {
super();
this.setLegajo_id(super.getLegajo_id());
}
//Getters and setters
}
Now, if I try to insert a new empleado into the database, it works just fine... BUT if I try to insert an empvarios, in the place where should be legajo_id, hibernate places the persona_cuil (I tested this by removing the FK restriction on the data base)
Images below:
(cant post images due reputation restriction :/)
https://www.dropbox.com/sh/mu5c797adlf7jiv/AACnd8mx7GriSyq5OMKoddRna?dl=0
There you have the 3 photos, the name of the files shows which table is each one.
Any ideas on whats going on?
The problem was that the data base was wrongly mapped.
If anyone has this problem then you will have to rethink the structure of the DB.
As seen in the example i gave above, the database should look like this:
persona:
persona_id (PK-autoincemental)
empleado:
persona_id (FK to persona)
legajo_id
empvarios:
persona_id (FK to persona)
ocupacion_id
The reason this works like this is because you cannot have different ids to depend different clases within the data base. On the program side it "can" work like that, but it the data base it has to be mapped differently.
Thanks!
I am trying to use ORMLite to represent comments in a conversation, like this:
#DatabaseTable(tableName = "comments")
public class Comment implements Parcelable{
#DatabaseField(id = true)
private Long id;
#DatabaseField
private Long conversation_id;
#DatabaseField
private String text;
...
public static class List extends ArrayList<Comment>{
}
}
...and...
#DatabaseTable(tableName = "conversations")
public class Conversation implements Parcelable{
#DatabaseField(id = true)
private Long id;
...
#ForeignCollectionField
private Comment.List comments;
#DatabaseField
private Date created_at;
...
}
And I am getting this error:
Field class for 'comments' must be of class ForeignCollection or
Collection
I am also using GSON so these models are populated automatically from json. For example:
{
"created_at":"2013-08-12T20:38:11Z",
"id":31,
"comments":[
{
"conversation_id":31,
"id":46,
"text":"IE sucks",
},
{
"conversation_id":31,
"id":47,
"text":"Yes it does",
}
]
}
Is there a way to achieve this just by changing descriptors?
Is it necessary to rework the Conversation class to use ForeignCollection as a type for comments or change Comment.List class to extend ForeignCollection? I'd like to avoid doing any of those because I am afraid it would break GSON implementation which currently works fine.
In Comment class:
...
#DatabaseField(
foreign = true
)
private Conversation conversation_id;
...
The conversation_id will actually only store id of the Conversation object, not the object itself.
There is a really good (if somehow unformatted) documentation here: http://ormlite.com/docs/foreign-object
I have the following Entity structure:
public abstract class BaseEntity {
private int _version;
public BaseEntity() {
}
#Version
#Column(name = "oplock", nullable = false)
private int getVersion() {
return _version;
}
#SuppressWarnings("unused")
private void setVersion(final int version) {
_version = version;
}
//some other code goes here....
}
the concrete Entity:
#Table(name="my_table", schema="public",
uniqueConstraints=#UniqueConstraint(columnNames={"username", "attribute"})
)
public class MyEntity extends BaseEntity implements java.io.Serializable {
private int id;
private String username;
private String attribute;
private String op;
private String value;
public MyEntity() {
}
//some code goes here ....
}
Now, I select from the database an entity of MyEntity type, lock it with entityManager.lock(myEntityInstance, LockModeType.OPTIMISTIC);, modify it, and persist modifications. The thing that I expect to get is to have oplock column's value incremented (oplock is the version column), but it is untouched.
Question: Does it behave right or am I missing something? I think the right behavior would be that the oplock's value would be incremented.
Edit: After I switched from hibernate 3.6 to hibernate 4.1, I still get the same behavior.
Finally I solved my problem:
I put only #MappedSuperclass annotation above BaseEntity class. For more details you can read here.