Using custom property in WHERE IN clause with $X{} in Jasper Reports - jasper-reports

I currently have a parameter called countries of type java.util.List and I'm using it in a WHERE IN clause as follows:
AND $X{IN, rd.country_id, countries}
In my app I must convert my List<Country> to List<Integer> so that I can use it to make the IN clause, otherwise Jasper would create something like country_id IN ("Argentina", "Brazil") instead of using the IDs.
I was wondering if its possible to use List<Country> as my parameter and telling $X{} to use the field id to create the IN clause so that I can use the toString() method to show what countries the user has selected, any idea? thanks

One method i can think of right now is to have a java class which will take your List of type countryname and return List of type countryId .
you have to make another parameter of type List.This parameter will call the java method.and then you can use this parameter in you query
I assume that the answer provided by #Lisa is not working in this case.
Edit:
Place the java class in your project classpath
The "<>" tags were not visible

I'm not sure how you're deploying this report, but could you set the input control's visible column to the country name, but set the value column to the country ID?
If you're unable to edit the input control, what about modifying your query to handle the country name to ID conversion?
SELECT *
FROM myTable
WHERE countryID IN (SELECT countryID
FROM countryTable
WHERE $X{IN, countryName, countries}
)

Related

Create Entity of specific columns

I am using Breeze (http://www.breezejs.com/) and to use the functionality I want it requires mapping to a complete entity and all of its fields. I have a "Person" entity, but it includes a Social Security Number field. I want to keep this SSN# field private so I would like to create an entity named SubSetPerson that is updateable, has navigation properties and only contains the columns I want (e.g. ID, FirstName, LastName, myNavigationProperty) and does not contain the SSN#. I am using database/model first. Is this possible?
If you are using database first, then you could create a view for that table which only selects the columns you want. Then update the EF model browser to include that view.
Try using a Master-Detail type structure for your person. The master table would contain the person's public information; ie name, birthdate, etc... The detail table would contain only the more sensitive information (SSN, etc...). Then depending on your needs you can load the detail or not.

Can I change the convention for string column generation by EF Code First?

Using a very specific example for ultimately a more general question: How can I configure the column type convention for Code First column generation to use nvarchar(50) instead of nvarchar(MAX) for string columns?
You can change the default db type two ways:
Firstly, you can add the appropriate attribute to the property. An annotation of [StringLength(50)] specifies a length of 50.
Secondly, you can configure the property using the StringPropertyConfiguration methods.
var entityTypeConfiguration = modelBuilder.Entity<YourEntityType>;
entityTypeConfiguration.Property(t => t.StringProperty).HasMaxLength(50);
You can check the other StringPropertyConfiguration methods to see what other options are available for configuration.

MyBatis mapping properties to database columns when inserting & updating (using annotations)

I'm just starting to learn MyBatis and I'm wondering, when I'm creating insert or update queries, is there a way that I can make property names a bit more friendly to refactoring? I'll explain in more detail:
I have constants in all of my domain classes that can be used to reference property names. In my opinion, it cuts down on typos and makes refactoring a bit easier.
public static final String FIRST_NAME = "firstName";
private String firstName = "";
When I create a MyBatis select statement using annotations, I can do something like this:
#Select("SELECT ID, FIRST_NAME, LAST_NAME FROM CUSTOMERS WHERE ID = #{id}")
#Results({
#Result(property = CustomerDetail.ID, column = "ID"),
#Result(property = CustomerDetail.FIRST_NAME, column = "FIRST_NAME"),
#Result(property = CustomerDetail.LAST_NAME, column = "LAST_NAME")
})
CustomerDetail selectById(final int id);
If I refactor my domain object (CustomerDetail) and change property names, it ends up being fairly simple.
However, when I create a MyBatis insert statement using annotations, I have to do something like this:
#Insert("INSERT INTO CUSTOMERS (ID, FIRST_NAME, LAST_NAME) VALUES (#{id}, #{firstName}, #{lastName})")
void insertCustomerDetail(final CustomerDetail customerDetail);
In this case, if I refactor my domain object (CustomerDetail) and change property names, it's much more error prone. Is there a way I can use my constants without resorting to a bunch of string concatenation? Is it something I should even concern myself with?
As a total newbie, I was expecting the #Insert and #Update annotations to mimic the structure of the #Select annotation. For example (please note, the below are NOT valid MyBatis annotations):
#Insert("INSERT INTO CUSTOMERS (ID, FIRST_NAME, LAST_NAME)")
#Params({
#Param(property = CustomerDetail.ID, column = "ID"),
#Param(property = CustomerDetail.FIRST_NAME, column = "FIRST_NAME"),
#Param(property = CustomerDetail.LAST_NAME, column = "LAST_NAME")
})
void insertCustomerDetail(final CustomerDetail customerDetail);
Have I missed any options that would have the same effect as my above sample? Alternatively, is it possible for me to unit test MyBatis mappings to ensure no one is using property names that don't exist in my domain objects? Testing may be a better solution since it would also apply to XML based mappings. Thoughts?
Is it something I should even concern
myself with?
I don't think so. I understand your concern, and I see how such a feature could be beneficial to you, especially early in development when POJO's tend to change so often.
I don't think your objects fields will be refactored enough to justify the investment needed to provide this functionality.
I suggest you read about the #Param tag in the mybatis user guide. It's not exactly what your looking for here, but it is a way to decouple object field names to mybatis sql map variables. In my mind, I would take your approach over passing in individual feilds with a #Param.
In regards to unit testing your sql maps, i'm fairly certain that if you use an ognl expression that doesn't have a corresponding get method in the object you'll get an exceptions. i.e if you use #{someField}, and the object your passing in doesn't have a getSomeField() method, then you get exception.

Access the property used in mapping entity to a table in EFv4

When we have two entities in EFv4 EDM diagram and only one table for both in the database (for instance, having table Documents and entities Invoice and Qoute), table Documents having documentTypeId column as a discriminator and set this column as a discriminator in the EDM (in Table mappings), how do we read the value of this property in our code?
We cannot assign values to it because EF does it for us under the hood (based on what we entered in Table mappings for condition) but somehow I don't get it why we are also not allowed to read it.
Imo this property is already mapped so you can't map it again. It is used to determine type of materialized entity. Why do you need such column. Usually it is enough to use is operator like:
var document = context.Documents.GetById(id);
if (document is Invoice)
{
...
}
If you only need to select subtypes you can use OfType extension method like:
var invoices = context.Documents.OfType<Invoice>().ToList();
You also don't need to set this value when adding new entity because you are adding subtype - Invoice or Quote.
Edit:
As I understand from your comment you don't need this information in query. In such case you don't need to map it. Simply use partial class of your entity and add custom property which will return your string. Sound like stupid solution but actually it would be the easiest one.
Discriminator column should be part of mapping metadata so in case of T4 template generating your entities, it could be possible to update the template so it generate such property for you.
You may want to use a single-table inheritance hierarchy, as described here.
That way, you could have an abstract Document class that includes a DocumentTypeId column. Invoices and Quotes would extend this class, but specify certain DocumentTypeId filters. However, because the original class has a DocumentTypeId column, they would each have that column as well.
Another advantage to this approach is that you could create utility methods that can act on any Document, and you could pass any Invoice or Quote to these methods.

Doubt regarding JPA namedquery

I am trying to execute a namedquery
#NamedQuery(name="getEmployeeDetails",query="select e.username,e.email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1")
Now when I execute this query in a EJB 3.0 Session Bean what is the object I should return.I tried returning Listits returning a Vector which creates a classcast exception.The employee table contains fields like password and other confidential details which I don't want to fetch.So I am not using select e from Employee e.
I am learning JPA can anyone help.
Below is the sample query which fetches only the required fields, but have to make such constructor for it.
Query : SELECT NEW package_name.Employee(e.username,e.email,e.image,e.firstname,e.lastname) FROM Employee e where e.empid=?1;
It will return Employee entity with selected fields & remaining will have default values.
Inspect the returned type by calling .getClass() on a returned object. I'd guess it's an array.
But this is not really a good way to use JPA. Select the whole entity and then just don't use what you don't need. It's not such a performance hit.