Java #Column-Annotation change the name of columns in Code - java

I have an Entity-Class (#Entity) where I have specified Columns(#Column) for OracleDB. It is possible to change the names of columns in code? And has it any affects to the existing database? Because the database contains actually lot of data.
I can't test it by myself now, because I do not have access to database actually.
Can anyone help me resp. can you give me some tips where can I search for a answer for my question?
Entity-Class like this:
#Entity
public class Example {
#Id
#GeneratedValue
private long id;
#Column
private String firstName;
#Column
private Date dateTime;
//Setter / Getter
}

If column in you db is as
#Column(name = "name_you_have_in_db")
If you will do
#Column(name="name_you_want")
then it is not possible because I guess hibernate doesn't track renames. It will create new field into the DB.
So solution is stop your app, alter your table and start your app again.

Related

Objectify Keys-Only Query?

I want to check if cert entity exist in the database using keys-only queries. So far I'm doing:
Iterable<Key<LikeMW>> liked = ofy().load().type(LikeMW.class).filter("likedObject", postKey).filter("user", userKey).keys();
post.setLiked(liked.iterator().hasNext());
So I have 2 questions:
1 - If I use ".first().now()" after ".keys()", does it switch from "keys-only" or it'll still be a "keys-only" query?
2 - Is there a better way to check if cert entity exist using "keys-only" queries and filter?
Thank you guys!
UPDATING
#Entity
public class LikeMW {
#Id
private Long id;
#JsonIgnore
#Index
#Load
private Ref<UserMW> user;
#JsonIgnore
#Index
private Key likedObject;
...
}
And one of possible liked objects...
#Entity
public class PostMW{
#Id
private Long id;
#JsonIgnore
#Load
private Ref<UserMW> owner;
#JsonIgnore
#Load
private Ref<MediaMW> media;
...
}
The only way to authoritatively look up whether an entity exists is to load it by key. You can certainly do a keys-only query, but it will be eventually consistent and will not guarantee that you do not create duplicates.
Given what you are trying to do, you will almost certainly be better off parenting LikeMW with the user and using the stringified likedObject as the string id. That way you can do a strongly consistent lookup and use transactions.

eclipselink struct fields order

I am working on a EclipseLink application, which uses Oracle Objects as IN and OUT parameters (while invoking stored procedure). As you know we have #Struct annotations available in Eclipselink for representing Oracle Object, I used it and it is working perfectly. But, looks like order of the fields declared in Struct annotated class matters a lot to map to correct field in oracle object. This causes maintenance issues and very difficult to code when object's properties are more. Is there a way in Eclipselink to say map Structure fields based on name and not with order.
Ex: Below is my Struct class. If by chance I declare variables in different order from fields list, wrong/incorrect mappings will happen while fetching records from stored proc. Its always mapping values to fields from top to bottom. #Column name annotation is not able to solve this issue.
#Struct(name = "REC_OBJECT",
fields = {"TRANS_ID", "PROJECT_ID", "LANGUAGE_CODE", "DESCRIPTION"})
#Embeddable
public class Master {
#Column(name = "PROJECT_ID")
private String projectId;
#JsonIgnore
#Column(name = "TRANS_ID")
private String transactionId;
#Column(name = "LANGUAGE_CODE")
private String languageCode;
#Column(name = "DESCRIPTION")
private String description;
}
Please suggest solution for this. Thank you.

Index Entities without direct relation in Hibernate Search

I'm trying to use Hibernate Search on two Entities, that do not (and must not) share a relation on object-level, however they're connected by a join table that uses their IDs. (legacy)
These are more or less the two Entities:
#Entity
#Indexed
class Person {
#Id
private long id;
#Field
private String name;
....
}
#Entity
#Indexed
class Address {
#Id
private long id;
#Field
private String street;
#Field
private String zip;
....
}
They are connected by their IDs:
#Entity
class Relation {
#Id
private long id;
private long personId;
private long addressId;
}
The goal I'm trying to achieve is finding similar persons that share a similar address via Hibernate Search. This means I'm searching for attributes from both Person and Address.
I guess the easiest way is to "emulate" an #IndexedEmbedded relation which means denormalizing the data and add "street" and "zip" from Address to a Person document. I stumbled upon Hibernate Search Programmatic API, but I'm not sure if that's the right way to go (and how to go on from to there)..
Would this be the proper way of doing things or am I missing something?
If you cannot add this relationship into the model, you will be pretty much out of luck. You are right that you would have to index the Person and corresponding Address data into the same document (this is what #IndexedEmbedded does really). The normal/best way to customize the Document is via a custom (class) bridge. The problem in your case, however, is that you would need access to the current Hibernate Session within the implementation of the custom bridge.
Unless you are using some approach where this Session for example is bound to a ThreadLocal, there won't be a way for you to load the matching Address data for a given Person within the bridge implementation.

"ORA-01754: A table may contain only one column of type LONG" with only one Long type

I am using hibernate with Oracle and tables are updated using hbm2ddl option at runtime before service starts.
Today, I started facing a weird issue where I came to know that table T has two long columns which is not supported in the Oracle.
#Table(name="table_t")
#Entity
public class T extends BaseEntity{
#Id
Long id;
#Column(name="col1")
Long col1;
#Column(name="col2")
Integer col2;
}
After facing the issue, I changed datatype of col1 to Integer(tables were not created in the DB) and tried re-running the application but it still fails with the same error.
Current schema appears like this:
#Table(name="table_t")
#Entity
public class T extends BaseEntity{
#Id
Long id;
#Column(name="col1")
Integer col1;
#Column(name="col2")
Integer col2;
}
Why is it happening? I tried cleaning, rebuilding and all.
Is meta info already stored somewhere? Or I am missing something else?
Thanks,
It's possible that the table has an unused long column.
You will not be able to see it but it needs to be dropped before you can add another one.
I have tried to explain it in this post:
http://international-dba.blogspot.co.uk/2014/01/ora-01754.html

#ManyToOne mapping fails to save parent ID

I'm using JPA2 with EclipseLink implementation
![Simple table structure][1]
Here are the two tables which I try to map and the JPA annotations.
public class Story implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
Integer id;
#Temporal(TemporalType.TIMESTAMP)
#Column (name="DATE_CREATED")
Date dateCreated;
String title;
String description;
#Column(name="AUTHOR_ID")
Integer authorId;
#Column(name="COUNTRY_ID")
Integer countryId;
private String reviews;
#OneToMany(mappedBy = "story", cascade=CascadeType.ALL)
private List<Tip> tipList;
}
public class Tip implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
private String description;
private Integer vote;
#ManyToOne (cascade=CascadeType.ALL)
#JoinColumn(name="STORY_ID", referencedColumnName="ID")
private Story story;
}
As a simple example I would like to persist a story and some story related tips in the same transaction.
Here is the section of code which does that:
Story newStory = new Story(title, body, ...);
EntityTransaction transaction = em.getTransaction().begin();
boolean completed = storyService.create(newStory);
//The tips are saved as a List<String>. This methods creates the needed List<Tip> from the Strings
List<Tip> tips = TipUtil.getTipList(tipList);
newStory.setTipList(tips)
transaction.commit();
I have no errors and all the entities are persisted in the database. The problem is that in the tip table the story_id field is always NULL. I can imagine that JPA is unable to get the new id from the story table. What's the correct approach here?
LE
In the current state of the code, the Tip entities are persisted but the country ID remains null.
With JPA, it is always recommended to update the relationship on both the sides in a bi-directional relationship. This is to ensure that the data is consistent in your application layer and nothing to do with the database.
However it is mandatory that you update the owning side of the relationship in a bidirectional relationship.
So, setting/not setting
story.setTipList(tips)
is up to you. But if you want the changes to reflect properly in DB then you mush call
tip.setStory(story)
as Tip is the owning side here, as per your code.
Also your code looks incomplete to me. Reasons is,
the entity returned by storyService.create(newStory) is managed but not the newStory. So just setting newStory.setTipList(tips) will not updated the db
Because you need to update the parent link story in each of your child.
The way its is done is to create a addTip(Tip tip) method in your Story class.
This method does :
tip.setStory(this);
tipList.add(tip);
If you don't need bedirectional approach, you can remove the story field in Tip and it will resolve your problem
Remove the
#Column(name = "STORY_ID")
private Integer storyId;
You are already declaring it in #JoinColumn(name="STORY_ID", referencedColumnName="ID")
That is why you are getting the error Multiple writable mappings exist for the field [tip.STORY_ID]
You should not be using PrimaryKeyJoinColumn, just JoinColumn, but having your complete class would help giving a certain answer.
PrimaryKeyJoinColumn would only be used if the story_id was also the id of the Tip (no id in Tip) and there was a duplicate basic mapping for it. It should rarely be used, and is not required in JPA 2.0 anymore as duplicate id mappings are no longer required.

Categories