I have a JSONObject in which I am putting keys and values from columns in my database. I want the key name to show up as a custom defined name, while I have it equivalent to the column name in the DB. How do I do this?
For eg, I have -
myObj.put(resultSet.getString(personId), personCarInfo);
While I want it to be -
myObj.put("personId", personCarInfo);
In JDBC, you can query the name of a column using the ResultSets meta data:
String columnName = resultSet.getMetaData().getColumnName(columnIndex);
String columnValue = resultSet.getString(columnIndex); // assuming, its a string
myObj.put(columnName, columnValue);
If I understood you correctly, you want to have mapping between column names and your custom defined names.
You could create simple one-directional mapping using statically initialized hashMap or by using enums.
If you want bi-directional mapping (i.e. be able to restore your table column name string from custom "personId" string) you could use Guava BiMap, for example.
Hope that helps.
Related
I want to filter entity by using #Where and want to know how to pass dynamic value in JPA
Example : #Where(clause = "name = 'Alas'") //Here 'Alas' is static i want to make it dynamic
You cannot make the structure of the WHERE clause itself dynamic in this way. Assuming you want to make certain portions of the WHERE clause dynamic, you could use:
SELECT *
FROM yourTable
WHERE name = ? OR ? IS NULL;
To the ? placeholders above, you would bind 'Alas'. If you don't pass any value, then the crtierion on name would simply be ignored.
We have some custom types that reflected to multiple db fields. For example
PersonName{
String salutation,
String firstName,
String lastName
}
stored as 3 separate db fields.
And it's boring to always write
db.select(PERSON.FIRST_NAME, PERSON.LAST_NAME, PERSON.SALUTATION, ... some other fields)
then fetch the record and create PersonName type from the appropriate record fields.
The idea is to define some multi-column custom field PERSON_NAME, which will be expanded by jooq into three "real" fields during the query execution, and packed to the one PersonName object in the result.
Looks like it's possible to do something like this with org.jooq.impl.AbstractField, but I'm wondering, may be there is a solution for such case already.
There are pending feature requests to support this kind of functionality:
https://github.com/jOOQ/jOOQ/issues/2360 (nested records)
https://github.com/jOOQ/jOOQ/issues/2530 (fetch groups)
With out-of-the-box functionality of jOOQ 3.6, you could store those columns somewhere as:
Field<?>[] personName = {
PERSON.SALUTATION,
PERSON.FIRST_NAME,
PERSON.LAST_NAME
};
And then select them as such:
db.select(personName)
.select(... some other fields);
I have a table T with columns defined as usual.
#Entity
#Table(name="T")
public class T {
#Column(name="test_id")
private Long testId;
}
Given entity property "testId", I want to get corresponding DB column name (i.e. "test_id"). How could it be achieved?
Edit 1:
I want to keep this column at separate location with actual DB column name (test_id) than testId. I fetched these values from DB using HQL which have key as entity name (i.e. testId) and I want actual column name in DB.
If I understood your requirement correctly, you want to use HQL while having a consistent name for both DB column and the entity field, like this:
SELECT t.test_id FROM Test t
instead of
SELECT t.testId FROM Test t
There is only one way to do that - renaming the field to test_id. HQL works on entities, not on DB tables, so you must use proper field names in the query.
Since test_id contradicts the usual Java coding conventions, I would advise against it.
EDIT: Getting the annotation attribute value with reflection would work along this outline:
Field field = MyEntity.class.getDeclaredField("testId");
Column a = field.getAnnotation(Column.class);
String columnName = a.name();
I would try to avoid this by any means, but if you're really sure you'll need it, use:
Configuration configuration = sessionFactory.getConfiguration();
PersistentClass persistentClass = configuration
.getClassMapping(T.class.getName());
String columnName = ((Column) persistentClass.getProperty("testId")
.getColumnIterator().next()).getName();
See also Get table column names in Hibernate
I saw a few questions in stackoverflow, but they all refer to old answers (from 2004) and using hibernate xml mappings.
I am writing an API in Java, which will return products which are stored in the database based on an algorithm.
The API would also get a locale and decide to return the name in the locale language.
Product table:
product_id
name
price
How should I support internalization?
Should I add name_en name_fr etc columns?
Or should I create different tables?
How should I return the name in the required locale in Hibernate?
I am using annotations for O/R mapping
I would suggest adding a column "lang" that contains prefix of language (ex. en, fr, ro, etc.) and a column "name" that contains the name in all languages separated by a separator like "," and parse the string.
I would remove the name column, and create a one-to-many relationship with a separate table named localized_name:
create table localized_name (
product_key int not null,
locale nvarchar(32) not null,
name nvarchar(255) not null
)
My experience is with JPA, not Hibernate, but I'm guessing the code looks similar in both:
public class Product {
private Collection<LocalizedName> names;
}
In JPA I would add a #Size(min = 1) annotation to the field to ensure it contains at least one element when it is persisted.
I have a field URL countryURL; in a Country class. I want to store its data into a COUNTRY table in a database through Hibernate.
Which Hibernate type I should use in the hibernate mapping file
<property column="COUNTRY_URL" name="countryURL" type="..."/>
It is not excepting string and text type.
You can create a org.hibernate.UserType for URL which maps to a varchar column. See Custom value types in the reference documentation.
You could write your own user type, but it might be easier to perform the conversion String<->Url in property getters and setters:
private void setRawUrl(String s) {
this.url = new Url(s);
}
private String getRawUrl() {
return url.toString();
}
and map the property rawUrl with type string.
it seems that there is for some times now a dedicated basic value type provided by Hibernate himself : org.hibernate.type.UrlType
I'd store it as String. No need to make your life harder by implementing a custom type. That is, change URL to Stringin Country.
Nope.. storing as String doing conversion our self is wrong by OOAD approach.. Once it is a string user can always give any string, which may not be URL.. better to use Custom T