JPA Access Constants from XML File - java

i'm trying to map some classes using the Hibernate JPA implementation. My problem is, that I can't use hardcoded Strings or constants in the xml-file you can see below.
It is also no possible to use a constant like in the name-attribute of the entity-tag. dst.ass1.jpa.util.Constants.T_CLASSROOM
this is the error message I get:
I don't know why I can use a constant in den name attribute of the entity tag, but not inside the column-tag.
I'm using IntelliJ IDEA 14.0.3
Hope you understand my problem.
regards.

For an attribute be used in an entity it must be "non-static".
If you map it in your XML, the JPA/Intellij will understand that you are trying to map an static attribute into the entity, and that is not possible.

Related

How to use special characters in XML tag like <abc-name> using JAXB?

I have one requirement where I need to generate XML from JAXB.
I have used basic object where we have only key value pair with parent child relationship but never encountered situation like below.
Expected XML
<swExtended>
<swx-mandatory>FALSE</swx-mandatory>
<swx-period/>
</swExtended>
In Java Object, I dont know how to name tag with special character <swx-mandatory> while defining java variable.
As BretC mention, Answer was very simple
#XmlElement(name = "dave-123")

Dynamically setting and getting bean properties in XPages

Just another Java problem (I'm a noob, I know): is it possible to use dynamic property binding in a Custom Control with a dynamic property getter in a Java bean?
I'll explain. I use this feature extensively in my Custom Controls:
<xp:inputTextarea id="DF_TiersM">
<xp:this.value><![CDATA[#{compositeData.dataSource[compositeData.fieldName]}]]></xp:this.value>
This is used in a control where both datasource and the name of the field are passed as parameters. This works, so far so good.
Now, in some cases, the datasource is a managed bean. When the above lines are interpreted, apparently code is generated to get or set the value of ... something. But what exactly?
I get this error: Error getting property 'SomeField' from bean of type com.sjef.AnyRecord which I guess is correct for there is no public getSomeField() in my bean. All properties are defined dynamically in the bean.
So how can I make XPages read the properties? Is there a universal getter (and setter) that allows me to use the name of a property as a parameter instead of the inclusion in a fixed method name? If XPages doesn't find getSomeField(), will it try something else instead, e.g. just get(String name) or so?
As always: I really appreciate your help and answers!
The way the binding works depends on whether or not your Java object implements a supported interface. If it doesn't (if it's just some random Java object), then any properties are treated as "bean-style" names, so that, if you want to call ".getSomeField()", then the binding would be like "#{obj.someField}" (or "#{obj['someField']}", or so forth).
If you want it to fall back to a common method, that's a job for either the DataObject or Map interfaces - Map is larger to implement, but is more standard (and you could inherit from AbstractMap if applicable), while DataObject is basically an XPages-ism but one I'm a big fan of (for reference, document data sources are DataObjects). Be warned, though: if you implement one of those, EL will only bind to the get or getValue method and will ignore normal setters and getters. If you want to use those when present, you'll have to write reflection code to do that (I recommend using Apache BeanUtils).
I have a post describing this in more detail on my blog: https://frostillic.us/f.nsf/posts/expanding-your-use-of-el-%28part-1%29

How to use reflection to retrieve private variable property from JPA objects

One of my goals is to create an engine that will set values in pojo object from JPA objects dynamically using reflection. One of the matching criteria is, that the field names should match.
I was successfully able to implement this for two pojo objects. But when I tried using JPA objects as one of the object parameter, it didn't work. Based on my research I found out that the method Class.getDeclaredFields() , does not give me the name of the field but the getter/setter method name of member variable for JPA objects.
Can anyone please give me a lead or direction as in where/what should I look to accomplish this task?
JPA providers will often use dynamic proxy classes of your concrete JPA classes, so you have no guarantee of the field names in the proxy. The only guarantee about a proxy is that the methods are the same. Use a debugger to inspect the runtime class of the JPA class instances that you're trying to use and you'll see the problem.
The best you'll be able to do is use reflection to call methods on JPA-returned objects.
All that aside, I don't really see why you'd need to POJO-ify an entity class anyway, since an entity is primarily an annotated... POJO.
One of the matching criteria is, that the field names should match.
I think that this is the root of your problem. There is simply no guarantee that a Java object's field names will match the names of getters and setters ... or anything else. If you make this assumption, you will run into cases where is doesn't work.
The best solution is to simply not use this approach. Make it a requirement that the Pojo classes conform to the JavaBeans spec and rely on the setters to set the properties. This is likely to work more often than making assumptions about (private) field names.
In fact, the state of a generic JPA object implemented using a dynamic proxies could well be held in a hash map. Those fields you can see could simply be constants used for something else.

Mapping a string (key) to an XML schema (value)

I am trying to map a String (key) to an XML schema with few variable that will change (marked with $ sign).
I am not sure what is the best way to do this in terms of the map to use (hashmap?) and the type to use for the xml schema (map a string to ..?)
Also, I am not entirely sure if passing the strings that will change (let's say: id and name for example) as part of the key and replace them or get the schema back and replace the variable once I process the schema again.
Any help is appreciated.
Instead of implementing this on your own, bind the schema types to Java classes or use some sort of templating engine like StringTemplate.
So is it not simplest to treat the Schema as a string, and just hold it in a Map. Then when you pull it out, do a replace/replaceAll on the schema, and return it. I think that covers what you want?

How to create enum dynamically?

I need to create an enum based on a table from the database.
DB table MyColors: id/title/value
1/Red/1
2/Green/4
dynamic create
enum MyColors {
Red=1,
Green=4;
}
You can dynamically create source code by reading from the database and simply outputting the results in a format conducive to building an enum. However, it is impractical to create an enum at run time. You would be better off with some kind of associative array.
Actually there is a possibility of dynamically creating enums using reflection: http://niceideas.ch/roller2/badtrash/entry/java_create_enum_instances_dynamically
One option to is to define an XML Schema and the required values as enum and generate the class files, so that we can manage the values outside the source code, however we cannot dynamically generate the enum values from the database.
It's not clear if you want to generate source code or not. I guess not, since even compiled no code in the same program could access the the enum objects except through reflection.
So why not mapping the table to a ColorEntity object using JPA?
You can then have a list or a map of these entities or whatever you need.

Categories