Hibernate's PhysicalNamingStrategy's toPhysicalSchemaName does not get called - java

I'm using Hibernate and trying to externalize the entity's table and column names to a .properties file. I've implemented the PhysicalNamingStrategy to map the entity properties and when executed everything works correctly (i.e., the methods toPhysicalTableName and toPhysicalColumnName get called for the table name and every mapped property, respectively).
However the toPhysicalSchemaName doesn't get called, even if I provide the annotation #Table(schema="SCHEMA_NAME"). The documentation says that
The PhysicalNamingStrategy will be applied regardless of whether the attribute explicitly specified the column name or whether we determined that implicitly. The ImplicitNamingStrategy would only be applied if an explicit name was not given.
I do not know if this applies to the schema name too. The toPhysicalSchemaName just won't get called no matter what I do. Should I be configuring the schema name differently for this to work?
Appreciate the help!

After some debugging found out that the hibernate-core 5.2.2.Final incorrectly creates the namespace. The org.hibernate.boot.model.relational.Namespace.java constructor has the following line
this.physicalName = new Namespace.Name(
database.getPhysicalNamingStrategy()
.toPhysicalCatalogName(name.getCatalog(), database.getJdbcEnvironment()),
database.getPhysicalNamingStrategy()
.toPhysicalCatalogName(name.getSchema(), database.getJdbcEnvironment())
);
The Name constructor expects the second argument to be the schema name, but the catalog gets passed again. Instead it should have been
this.physicalName = new Namespace.Name(
database.getPhysicalNamingStrategy()
.toPhysicalCatalogName(name.getCatalog(), database.getJdbcEnvironment()),
database.getPhysicalNamingStrategy()
.toPhysicalSchemaName(name.getSchema(), database.getJdbcEnvironment())
);
This is fixed in hibernate-core-5.2.10.Final.

Related

Sipios' Spring Boot JPA Search - Querying Enum

I am using Spring Boot (2.7.3) and Sipios' library for Spring Search for JPA Entities (https://github.com/sipios/spring-search).
I am trying make a search based on one of my class's enum fields, but receive the following error:
InvalidDataAccessApiUsageException: Parameter value [FIRST] did not match expected type [*.MY_ENUM (n/a)];
To make it clear through an example, let's say I have an entity-class "MyClass" which has a field "MY_ENUM enumField", and MY_ENUM has the values ('FIRST', 'SECOND', 'THIRD').
I am trying to query for all MyClass entries that have the value 'FIRST' in their enumField.
The enumField is annotated with #Enumerated(EnumType.STRING).
For those familiar with the specific library, my HTTP request is: localhost:8080/controller/search?search=enumField:FIRST.
I have also tried putting the FIRST query param in single & double quotes but the result was the same.
I have traced the problem to Hibernate's org.hibernate.query.spi.QueryParameterBindingValidator, method isValidBindValue().
Method isValidBindValue() checks whether value "FIRST" is of MY_ENUM type.
More specifically, the line that effectively causes the exception is expectedType.isInstance(value), where expectedType is "class MY_ENUM" and value is "FIRST", which evaluates to false and thus the error message - values checked via Intellij's debugger.
I have tried it with versions 0.2.3 & 0.2.4 of sipios/spring-search.
I saw that version 0.2.3 should support querying enums but I couldn't find any relative examples on how to do it, so it may be possible I have misunderstood how to use spring-search, but I would expect Spring Boot to recognise the "FIRST" string as an instance of MY_ENUM type.

Is it possible to retrieve the entire list of named AnnotationSets in GATE?

is it possible to retrieve the entire list of an Annotation Set in GATE? This line of code returns all the items of a GATE document that belong to the AnnotationSet called "EMail";
AnnotationSet annSet = doc.getAnnotations().get("EMail");
Now, how can I know all of the Annotations' Set names instead of the only "EMail"?
Isn't this the answer to your question:
AnnotationSet annSet = doc.getAnnotations();
I think you mix two different terms: annotation set and annotation type. Be careful about these two...
There are several methods of gate.Document and gate.AnnotationSet that can be used:
gate.Document.getAnnotations() ... all annotations from the default (without name) annotation set.
gate.Document.getAnnotations(String setName)... all annotations from named annotation set with name setName.
gate.AnnotationSet.get(String anntoationType)... select only annotations with given anntoationType.
gate.Document.getAnnotationSetNames()... get all the names of named annotation sets in a document
gate.AnnotationSet.getAllTypes()... get set of all annotation type names of annotations present in given annotation set.
See more details in javadoc:
SimpleAnnotationSet
SimpleDocument
Document doc;
// create/manipulate the document...
Set<String> names = doc.getAnnotationSetNames();
Map<String, AnnotationSet> namedAnnSets = doc.getNamedAnnotationSets();
// The default AS always exists
AnnotationSet defaultAS = doc.getAnnotations();
Note that a GATE document always has a default (unnamed) annotation set, which is not included in the set of names or map of named sets.

Hibernate use column name for property name

I am trying to generate JPA's from hibernate during my build process and everything is working fine except for the naming. The default it looks like is hibernate uses the class type for the property name. Is there a way to make it use the column name in the database as the property name?
for example in my DB i have a column name customer_org_type which references a table called valid_value to make sure the type is valid. When I generate the table it creates a property called:
public ValidValue validValue;
but I would like it to generate as:
public ValidValue customerOrgType;
Is there any way to have that?
I figured it out.
It required an entry in my hibernate.reveng.xml
Since it was a foreign key reference I just had to add the following:
<table catalog="my_catalog" name="user_info">
<foreign-key constraint-name="customer_org_type_id_fk" >
<many-to-one property="customerOrgType" exclude="false" />
</foreign-key>
</table>

Hibernate criteria list iterating

I have the following section of code that I want to use to return a collection of my object:
Session session = HibernateUtil.getSession();
List<MyObj> myObjList = (List<MyObj>)
session.createCriteria(MyObj.class)
.add(Restrictions.eq("searchField", searchField)).list();
Iterator<MyObj> myObjIt = myObjList.listIterator();
log.debug("list size: " + myObjList.size());
while(myObjIt.hasNext()){
MyObj myObj = myObjIt.next();
log.debug(myObj.getMyField());
}
However, my log keeps printing the same record as many times as the size of the list. If I refactor slightly, my code works correctly like this:
SQLQuery query = session.createSQLQuery(
"select my_field from my_table where search_field = :searchField"
);
query.setParameter("myField", myField);
List result = query.list();
for(Iterator iter = result.iterator(); iter.hasNext();){
Object[] row = (Object[]) iter.next();
log.debug(row[0]);
}
Am I doing something wrong in my first code segment? I should be able to go about this either way, and since I'm using Hibernate, I'd rather the ORM be working as expected, so I'd prefer the former method over the latter. Anyone have any thoughts?
Fwiw, I am using Hibernate 3.5.4 final, Hibernate-validator 4.2.0 Final, hibernate-search 3.4.0 Final, and hibername-c3p0 3.6.5 final, all from the maven repos.
Edited to clarify based on comments.
From what you have described in the question, your both code segments should return the same results. Assuming that in first code segment Hibernate executes pretty the same query as in second segment (you can check it in log, just enable 'hibernate.show_sql' config parameter) - problem is somewhere in converting result set to MyObj list. It is pretty unlikely that it happens due to a bug in hibernate, so it can be due to incorrect entity class mapping. If you do not see any problems with mapping, please add more details to the question (your entity class with mappings, db table schema and data sample) so anyone can reproduce the issue.
Likely your MyObj class does not have Id column mapping properly defined. For example if the field/property mapped as Id has the same value for all the objects in the result list, hibernate will return same objects (as in your case).
Regarding using primitives as Id type: hibernate allow using primitive types, but it has the following line in the docs:
We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type.
Summarizing possible Id mapping issues:
1. Id mapped column is not unique in db.
2. Corresponding setter for Id property getter mapped is not specified or does not really save passed argument value.
3. Not-nullable type of Id field.

Creating custom user attributes in Active Directory using JNDI

I am attempting to create a custom attribute that can be assigned to an existing Active Directory user in my domain. I am not fully aware of how to achieve this. It is my understanding that once the attribute has been created, I can assign it to the user via:
mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("attributeName", "attributeValue"))
ctx.modifyAttributes(userDN, mods)
Any information is appreciated.
Not sure what you want to do.
But Active-Directory is a Directory, so it use a SCHEMA to define which attributes can be used in an object. This means that you can modify (add, delete, replace) the value of an attribut that exists (in the SCHEMA) for a given class, but can'nt add a custom attribut to a class without modifying the SCHEMA.

Categories