fetching a Key from a class into an another class - java

Lets say I have a class in which i generate a DB with some keys, now I want to have an other class in which i use for example the "key123" as I named it in the following, how can I do that? Is that possible to just copy&paste the #Basic and public String key123 in that other class? Will that Basic annotation fetch it from the DB into that class? even when they both have different packages?
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic
#XmlAttribute
#XmlID
private Long id;
#Basic
private String someotherKey;
#Basic
public String key123;

You probably need to use the MVC model of development
Class Keys - the DB Table as an object using the #Entity annotation etc
Class KeysDao - The Data Access Object that does all the DB calls keysDao.update(Keys), keysDao.add(Keys), keysDao.get(id) keysDao.getAllKeys() etc.
Class KeyService - this has the Business Logic so you can say in any other class keyService.getKey(id) and get the Key from the DB returned. Then you can get it in any other class you wish.
If you mean get in another table you need to look at SQL table joins and create your query correctly (or create a view in the DB - not a lot of people know but you can update and add using a View meaning you can make one commit with the data as you use in the view and not have to even know the real normalised DB beneath.

Related

How to keep current and previous location field in database?

I am implementing solution to office relocation. One of the main task is to show where assets/furniture should be moved. I have an entity like this (shorted version):
#Entity
#Table
class Asset extends BaseEntity{
private String name;
#Embedded
private Localization localization;
Localization contains Floor (floor_id, floor_name), x_axis, y_axis;
Here is a graphic representation of my problem (on frontend side).
How I can keep current and next (destination) localization in database? I've tried some ways but they did not work. Like here:
#Entity
#Table
class Asset extends BaseEntity{
private String name;
#AttributeOverrides({
#AttributeOverride(name="floor.id", column=#Column(name="current_floor_id")),
})
#Embedded
private Localization localization;
#AttributeOverrides({
#AttributeOverride(name="floor.id", column=#Column(name="destination_floor_id")),
})
#Embedded
private Localization destinationLocalization;
But it yells at me that floor_id is not unique and should be inserted=false and updatable=false.
Another way that I've tried was #OneToOne relation with new entity Column, or even joining in query without relations.
I've heard about Hibernate Envers, maybe that is solution?
If you need just 2 locations, I would suggest you use the approach you already mentioned i.e. keep both old and current location infos in the entity.
If you need a history, you could create an entity e.g. AssetLocation with a surrogate id and refer to that in the Asset. Just don't delete the old AssetLocation objects, then you can create a one-to-many association to refer to the old positions.
You could use envers as well, but there are some limitations you have to be aware of which you can read about in the documentation.

Storing same class objects in both Neo4j and MongoDB ssing Spring Data

The real issue is that both tools use #Id types while I can not create that kind of an object stored in both.
class DataBaseClass {
#GraphId #Field("graphId")
Long graphId;
#Indexed(indexName = "mongodb", indexType = IndexType.FULLTEXT)
#Id
String _id; //mongodb id
}
Neo4j #GraphId extend #Id so there is no difference between the two,
at first I thought changing graphId type from Long to String will will do the trick but I get casting exception error.
I also tried to use mongodb #Field annotation with no differ resoults.
In my humble opinion this prevention is bug in the Spring Data framework, that could be spared from Neo4j if the data layer could accept String id with internal casting.
I think I got the issue covered but I would like to check if there is a way to do this after all.

Persisting third-party classes with no ID's

Say I have the following Java class, which is owned by a vendor so I can't change it:
public class Entry {
private String user;
private String city;
// ...
// About 10 other fields
// ...
// Getters, setters, etc.
}
I would like to persist it to a table, using JPA 2.0 (OpenJPA implementation). I cannot annotate this class (as it is not mine), so I'm using orm.xml to do that.
I'm creating a table containing a column per field, plus another column called ID. Then, I'm creating a sequence for it.
My question is: is it at all possible to tell JPA that the ID that I would like to use for this entity doesn't even exist as a member attribute in the Entry class? How do I go about creating a JPA entity that will allow me to persist instances of this class?
EDIT
I am aware of the strategy of extending the class and adding an ID property it. However, I'm looking for a solution that doesn't involve extending this class, because I need this solution to also be applicable for the case when it's not only one class that I have to persist, but a collection of interlinked classes - none of which has any ID property. In such a scenario, extending doesn't work out.
Eventually, I ended up doing the following:
public class EntryWrapper {
#Id
private long id;
#Embedded
private Entry entry;
}
So, I am indeed wrapping the entity but differently from the way that had been suggested. As the Entry class is vendor-provided, I did all its ORM work in an orm.xml file. When persisting, I persist EntryWrapper.
I don't have much experience with JPA, but I wouldn't extend your base classes, instead I would wrap them:
public class PersistMe<T> {
#Id
private long id;
private T objToWrap;
public(T objToWrap) {
this.objToWrap = objToWrap;
}
}
I can't test it, if it doesn't work let me know so I can delete the answer.

Serializing List of POJOs using Hibernate

I have a Java class that is mapped to a database table using JPA. Inside this class I have a List<Code> that I need to store in database.
So the Code class is not mapped into Hibernate. Is there a way to Serialize the List<Code> without mapping the Code class into Hibernate? Thanks in advance.
Error Message:
org.hibernate.exception.SQLGrammarException: could not insert collection [com.app.Account.codes#2]
Problem is that I'm getting error when Hibernate attempts to Serialize my List.
#Entity
#Table (name="account", catalog="database1")
public class Account{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column (name = "id")
private String id
#Column (name = "type")
private String type;
#CollectionOfElements
#Column (name = "codes")
List<Code> codes;
...
...
}
public class Code implements Serializable{
//This is a basic POJO that is not mapped into Hibernate. I just want this object
//to be stored in database.
}
You need to annotate the codes field with #Lob, not with #CollectionOfElements.
See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#d0e6099 and the following paragraph.
This is a very fragile way of persisting a list of objects, though, because it uses binary serialization, which might quickly become non-backward compatible if the list or the Code class changes. It's also impossible to query or inspect using database tools.
I would rather map the Code class as an entity or as an embeddable.

Extend entity classes with composite keys in hibernate

In our company we have a strange database model which can't be modified because to many systems works with them. Up to know we have a straight java application which connects with hibernate to the database and loads the data. We have for each table one xml mapping file.
The strange thing about the database is that we do not have any primary keys. Most table have a unique index containing several columns.
Now we want to use an application server (jboss) and the ejb model. So I created a class like this:
#Entity
#Table (name = "eakopf_t")
public class Eakopf implements Serializable {
#Embeddable
public static class EakopfId implements Serializable {
private String mandant;
private String fk_eakopf_posnr;
// I removed here the getters and setters to shorten it up
}
#Id
private EakopfId id;
private String login;
// I removed the getters and setters here as well
}
This works perfect.
Because our customers have different versions of the database schema I thought about extending this class on each database release change. So each interface we create with java can decide which version of the table will be used.
Here is the extended table class
#Entity
#Table (name = "eakopf_t")
public class Eakopf6001 extends Eakopf implements Serializable {
private String newField;
// getters and setters
}
If I use Eakopf (the base version) it is working if I do something like that:
EakopfId id = new EakopfId();
id.setMandant("001");
id.setFk_eakopf_posnr("ABC");
Eakopf kopf = (Eakopf) em.find(Eakopf.class, id);
But if I do this:
EakopfId id = new EakopfId();
id.setMandant("001");
id.setFk_eakopf_posnr("ABC");
Eakopf6001 kopf = (Eakopf6001) em.find(Eakopf6001.class, id);
this exception occues
javax.ejb.EJBException: javax.persistence.PersistenceException:
org.hibernate.WrongClassException: Object with id:
de.entity.Eakopf$EakopfId#291bfe83 was not of the specified subclass:
de.entity.Eakopf (Discriminator: null)
Does anybody has an idea?
many greetings,
Hauke
Doing what you did means to Hibernate that you're storing two different kinds of entities in a single table. This is possible is you use a discriminator column. But if I understand correctly, you just want one kind of entity in the table : Eakopf6001. In this case, its base class should be annotated with #MappedSuperClass, not with #Entity.
I would suggest creating a class annotated with #MappedEntity (let's call it BaseEakopf), and two entities: EaKopf and EaKopf6001, each with their set of additional fields. Include one of the other of the entities in the list of mapped classes, depending on which one you want to use.
My personal opinion is that if you have multiple versions of your app, they should use the same entities, but with different fields. Your version control system would take care of these multiple versions, rather than your source code (i.e. have one set of source files per version of the app, rather than one single set of source files for all the possible versions).

Categories

Resources