Monday, November 7, 2016

Class.getDeclaredMethod with primitive types (basic types)

Lets have to get method object of this function:
    private void setI(int i) {
        this.i = i;

Sending Integer.class in argument list cause throwing an exception: java.lang.NoSuchMethodException:

The correct way is this:
    Method iMethod = progClass.getDeclaredMethod(
        "setI", new Class[]{ Integer.TYPE } );

Same pattern could be applied for byte, short, long, float, double, boolean, char.

Thursday, July 28, 2016

Intel RAID: Create mirror RAID from existing disk

What you want

If you have Intel RAID, drive wish information and drive which have to become its mirror, this page is for you.

Drive 1: your information is here;
Drive 2: you want to use this for mirror.

Intel BIOS Options

Intel BIOS do not give you opportunity to use Drive 1 as source for the RAID. In the BIOS you could create RAID, but it will destroy all data on both drives. The options are:
1.Select Recovery Volume Option
2.Enable Only Recovery Disk – enables recovery disk if available and disables master
3. Enable Only Master Disk – enables master disk if available and disables recovery disk

The solution

You cannot do this in BIOS. You have to boot into Windows from Disk 1, install progam Intel xxx, which has the feature which you want. The syncing is 2 or 3 times slower than expected.

The result is that you will see in BIOS that both drives are in RAID.

Why generics of type are used?


Lets have these classes:

public class SuperClass {
   // some methods

public class SubClass extends SuperClass{
   // some methods

So we are allowed to do this conversion:

SuperClass x = new SupClass();


But, we are not allowed to make such conversion:

List <SubClass> listSubs = ...
List <SuperClass> listSupers = listSubs; // compile time error happens

Such a stupid language limitation! In this case, there is no clean solution. The bad solutions produce new List and reinserts all elements into it.


But we could use "? extends Generics" structure in this way:

List <SubClass> listSubs = ...
List <? extends SuperClass> listSupers = listSubs; // NO errors

Note: It is normal that ? extends Class structure allows only supclasses of the generics to be used. I was glad to understand that you could use the generics type itself there. So it is also possible:

List <? extends SuperClass> listSupers = new LinkedList(); // NO errors

Error: "class or interface without bounds" solved

    public List<? extends SuperClass> getObjects() {
        List<? extends SuperClass> result = new ArrayList<? extends SuperClass>(1); // error: "class or interface without bounds"
        result.add( theNeededObject ); // error: "not suitable method found for ..."
        return result;

Fixed error:
    public List<? extends SuperClass> getObjects() {
        List<SuperClass> result = new ArrayList<>(1);
        result.add( theNeededObject );
        return result; // it is automatically converted from <SuperClass> to "<? extends SuperClass>"

Monday, March 14, 2016

Using date and time fields in entity objects

Temporal annotation shows which part of the datetime to be stored to database:
    @Temporal(TemporalType.TIMESTAMP) // date & time
    @Temporal(TemporalType.DATE) // only date part
    @Temporal(TemporalType.TIMESTAMP) // only time part

Nice tutorial:

Monday, March 7, 2016

Named query examples

Here are some examples of Java Named Queries:

Define named queries:

        @NamedQuery(name = "MyClass.getBySomething",
                query = " select mc from MyClass mc " +
                        " where mc.something = :something"),
        @NamedQuery(name = "MyClass.getBySomethingOther",
                query = " select mc from MyClass mc " +
                        " where mc.somethingOther = :somethingOther")

public class MyClass  {
  // ...

Get single result:

    public static MyClass getBySomething( xxx something, EntityManager em ){
        TypedQuery<MyClass> query = em.createNamedQuery("MyClass.getBySomething", MyClass.class);
        query.setParameter("something", something);
        try {
            return query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        } catch (NonUniqueResultException e) {
            e.printStackTrace(); // do nothing ...
            return null;

Multi-result query:

    public List<DsAisFlight> getAllUnarchived() {
       TypedQuery<DsAisFlight> query  = entityManager.createNamedQuery("MyClass.getAllUnarchived", MyClass.class);
       query.setParameter("something", something);
       try {
           return query.getResultList();
       } catch (NoResultException e) {
           return new ArrayList();

Update query:

    public static void updateXxx( EntityManager em ){
        TypedQuery query = em.createNamedQuery("MyClass.someUpdateQuery",
        query.setParameter("someValue", 1);
        int updatedRows = query.executeUpdate();
 // ...

Count query

@Table( name = "my_class" )
        @NamedQuery(name = "MyClass.countAll",
                query = "select count(l) from MyClass l")
public class MyClass {
    public static long countAll( EntityManager em ){
        Query query = em.createNamedQuery("MyClass.countAll");
        try {
            return (Long)query.getSingleResult();
        } catch (NoResultException e) {
            return 0;
        } catch (NonUniqueResultException e) {
            throw new UnsupportedOperationException("this is not possible");

Thursday, January 14, 2016

Showing table columns ordered by name

Here is SQL syntax for showing table columns ordered by name:

WHERE table_name = 'tablename'
ORDER BY column_name;

To show only name, type and schema:

SELECT column_name, column_type, table_schema
WHERE table_name = '

ORDER BY column_name;

JAXB: Marshalling entity to String example

 Here is an example / template for marshalling entity to String:

 Marshalling code fragment sw = new StringWriter();

JAXBContext pContext = JAXBContext.newInstance(SomeEntity.class);

Marshaller marshaller = pContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(dto, sw);


@XmlRootElement(name = "result")
public class SomeEntity{
    private String someVar;
    private String someOtherVal;
    // getters & setters