MyBatis: underscore not mapped to camelcase - mybatis

My problem is simple. I have a column name product_name in my product table in my mysql database but in my Product class (java), camelcase is used in productName. MyBatis is not mapping product_name to productName. Any solution for this? I had no problem in Hibernate before but right now I need to use mybatis for development

I know this is old but for those that may come across this, MyBatis supports mapping underscores to camel case
config.xml
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
...
</configuration>
Then your sql can look like:
select product_name, product_description, ...
from products
or even just
select *
from products

Underscore to camel case mapping can be enabled in spring-based configuration through a customizable SqlSessionFactory, like that:
#Bean
#Primary
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactory factory = sessionFactoryBuilder().build();
factory.getConfiguration().setMapUnderscoreToCamelCase(true);
// other configurations
return factory;
}

You have to use <resultMap> tag in MyBatis to return the result. For example:
<resultMap id="result" type="userModel">
<result property="id" column="USER_ID"/>
</resultMap>
In the above code, in type="userModel" userModel is defined in a config file where there is a mapping of userModel with a model java class which will have the corresponding setter/getter method for id.
For more info on this, refer the following Doc:
MyBatis Doc

I had a very simple solution for this, but I do not consider it proper solution.
I added as to match with a property of my class.
select product_name as productName from products;

Related

Item Descriptor Inheritance in ATG for "contactInfo"

I am trying to inherit the "contactInfo" item and create a new item descriptor.. Something like this as given below..
<item-descriptor name="testContactInfo" super-type="contactInfo">
<table name="test_contact_info" type="auxiliary" id-column-name="contact_id" shared-table-sequence="1">
<property name="fixedlinenumber" column-name="fixed_line_num" data-type="string"/>
</table>
</item-descriptor>
I get the following error when i start the server.
14:19:52,856 ERROR [ProfileAdapterRepository] Error parsing template: atg.repository.RepositoryException: Your item-descriptor definition for testContactInfo has super-type contactInfo but no sub-type attribute.
what am i doing wrong here? I have kept the definition in userProfile.xml
First question: are you actually looking to create a subtype of the contactInfo item descriptor - that is to say, are you expecting there to be some items in your system of type contactInfo and some items of type testContactInfo - or are you just looking to add a custom property to the existing contactInfo item descriptor?
If you are actually trying to create a subtype of contactInfo, then you need to modify the descriptor of contactInfo to tell it how to differentiate between items of type contactInfo and items of type testContactInfo. You will need to add a property, say contactType, to contactInfo and set the sub-type-property attribute
<item-descriptor name="contactInfo" sub-type-property="contactType" ...>
...
<property name="contactType" data-type="enumerated">
<option value="standard"/>
<option value="test"/>
</property>
...
</item-descriptor>
and then you can subtype it
<item-descriptor name="testContactInfo" super-type="contactInfo" sub-type-value="test">
...
</item-descriptor>
If, however, you are just looking to add a custom property to it, you can very well add to the existing definition. You do not need to subtype to extend an out-of-the-box item. For example
<item-descriptor name="contactInfo">
<table name="test_contact_info" type="auxiliary" id-column-name="contact_id" shared-table-sequence="1">
<property name="fixedlinenumber" column-name="fixed_line_num" data-type="string"/>
</table>
</item-descriptor>
will result in a new property called fixedlinenumber added to the standard contactInfo item.
Item-descriptor inheritance can be done in two ways. You can:-
Add new properties for existing item-descriptor.
Here you can add many properties to an existing item-descriptor. This can be out of the box, or your custom repository.
For example, you can have a employeeId property to contactInfo item-descriptor, which would be available for all contactInfo items.
Create a sub-type of an item-descriptor.
This is generally used to have distinctive properties for a particular item-descriptor.
For example, in your contactInfo type, you can have a "employeeContactInfo" wherein you want to store an extra employee id, and you can have a "employeeId" only for this type.
So, it basically depends on your requirements. You can see some details on this website.. nice tutorials:-
http://learnoracleatg.blogspot.in/2014/11/art203-how-to-extend-out-of-box-non.html
and
http://learnoracleatg.blogspot.in/2014/12/art204-how-to-add-new-item-descriptor.html

Seam file component in javaee6

I'm trying to replicate the loading of a property file of seam in javaee6 but still don't know where to start.
In seam we can load a property file as a seam component by defining it in the components.xml:
<component name="propertyBean" class="PropertyBean" scope="application" auto-create="true" startup="true">
<property name="filename">myPropertyFile.properties</property>
<property name="reload">true</property>
</component>
And then we can access it in code:
PropertyBean.getInstance().getProperty("myProperty");
Is there a javaee6 feature that will replicate this functionality? Or in spring it's called PropertyPlaceholder.
In c#, we can do it by adding configuration property in appsettings.xml. And access via ConfigurationManager.
Thanks,
czetsuya
Unfortunately, there's nothing like a property component manager from seam into javaee6, but I was able to find something similar, a property loader.
It works by having a qualifier:
#Qualifier
#Retention(RUNTIME)
#Target({METHOD, FIELD, PARAMETER, TYPE})
public #interface ConfiguredBy {
#Nonbinding public String value();
}
With a parameter that serves as the name of the property file.
The whole approach is describe here:
http://john-ament.blogspot.com/2010/03/writing-property-loader-in-java-ee-6.html

Using custom javax validation annotations in GWT

For the current project we're using JSR-303 annotations to validate our interface parameters.
I needed to create a custom annotation for dates, as the default #Past and #Before also take into account the time.
This is the definition of my annotation:
#Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
#Retention(RUNTIME)
#Documented
#Constraint(validatedBy = { NotInFutureValidator.class })
/**
* Annotated element must be a date before tomorrow, compared to the system's date
*/
public #interface NotInFuture {
String message() default "{be.credoc.contractRegistration.interface_v2.validation.pastignoretime}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
The implementation of the validator is pretty straight forward.
Our webapplication is written in GWT and it makes use of gwt-validation. This allows us to validate our parameters on client side by means of the same annotations.
However, when I annotate my parameters with my custom annotation and I want to validate it by making use of the GwtValidatorFactory, it doesn't give me an error when the input date is in the future.
Does anyone have experiencing defining and using their own annotations in a GWT application and can see what I'm missing?
Thanks in advance
Instead of creating new annotations you might try to re-define the validators for the existing #Past and #Future annotations by providing an XML constraint mapping like this:
<?xml version="1.0" ?>
<constraint-mappings
xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd">
<constraint-definition annotation="javax.validation.constraints.Past">
<validated-by include-existing-validators="false">
<value>x.y.z.NotInFutureValidator</value>
</validated-by>
</constraint-definition>
</constraint-mappings>
This mapping must be registered in the configuration file META-INF/validation.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">
<constraint-mapping>META-INF/validation/custom-constraints.xml</constraint-mapping>
</validation-config>
You can learn more about custom validators for existing constraints in the Bean Validation specification and the Hibernate Validator reference guide.
Apparently we wrote our own JavaScript implementations for each custom annotation, so the solution in this case was to write such an implementation.

Ignore optional columns on save

When I update a table from a Form through Autogenerated EF, if I remove some data-columns from the view form because I don't want to be editable, that columns are updated with null value, how can avoid this behavior? I read here: Entity Framework: Ignore Columns removing it from the model, but not always I want to ignore these datacolumns.
thank!
Another approach is to use annotations
[HttpPost]
public virtual ActionResult Edit(
[Bind(Prefix="", Include="field1", Exclude="field2")]MyClass myClass)
{
....
asp.net MVC provide you with UpdateModel method, look on the overload
protected internal void UpdateModel<TModel>(
TModel model,
string prefix,
string[] includeProperties,
string[] excludeProperties
)
where TModel : class
using it you are able to exclude or include particular properties by their names

quoting problem with JPA2 (EclipseLink) and PostgreSQL

With PostgreSQL, I have to double-quote all identifiers or they'll be implicitly lower-cased. I'd prefer to preserve case since "lastLoginAttemptIpAddress" is so much more readable than "lastloginattemptipaddress".
I have created an orm.xml file with (full contents below). That caused EclipseLink to quote most of the identifiers, but it specifically did not quote the column name when defining a foreign key constraint. How can I tell EL to quote all identifiers?
I have also tried using quotes in the explicitly specified table / column names to cause EL to quote identifiers. First and foremost, that doesn't work either -- same behavior. In addition to that, (1) that forces me to specify names twice (I already did so in the property accessor names) and in a manner that is invisible to refactoring tools, (2) it is wrong -- the quotes aren't part of the name, (3) it forces me to fix quoting at POJO level when it is actually a specific trait of the database system I am using.
orm.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd">
<description>description here</description>
<persistence-unit-metadata>
<persistence-unit-defaults>
<delimited-identifiers />
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
entity class:
#Entity
public class UserX {
...
#Id
#GeneratedValue(generator = "UserX_id_seq")
#SequenceGenerator(name = "UserX_id_seq", allocationSize = 1)
public int getId() { ... }
...
#ManyToOne(fetch = FetchType.LAZY, optional = false)
public UserX getModificationUser() { ... }
}
the generated query (note the missing quotes around the column name):
ALTER TABLE "UserX" ADD CONSTRAINT "FK_UserX_modificationUser_id" FOREIGN KEY (modificationUser_id) REFERENCES "UserX" (id)
Seems to be a bug that the foreign key column are not quoted. Please log the bug in EclipseLink and vote for it.
Does the DDL fail? A work around would be to define the DDL in a script, or switch just that column to be all lower case.