Tag Archives: java

timestamp with jpa

so i have my jpa project with some really nice DAOs and i was really happy… all unit tests are up and running and everything looked good… but it wasnt…

i store timestamp(6) in the oracle db and the mapping class looks good and correct:

@Column(name = “TIME_ADDED”)
@Temporal(TemporalType.DATE)
private Date timeAdded;

the @Temporal defines the mapping type… and actual DATE is really only the date… TIMESTAMP is the thing i wanted…

The @Temporal annotation denotes, whether annotated property should be mapped to java.sql.Date (holds only date part), java.sql.Time (holds only time part) or to java.sql.Timestamp (holds date and time also with fractions of second). It has nothing to do with generation of property value.
(http://stackoverflow.com/questions/2591570/hibernate-reverse-engineering-procedure-generated-temporaltemporaltype-timestam)

and btw. unit tests will not find these errors… due to jpa caching… 🙁

foreach in java

for(String item : list){
       // do things with the item
}

accessing imap

accessing imap mailboxes is super easy with javax.mail… 

final Properties props = new Properties();
props.setProperty(“mail.store.protocol”, IMAP_PROTOCOL);
final Session session = Session.getInstance(props);

final Store store = session.getStore(IMAP_PROTOCOL);
store.connect(server, user, password);

final IMAPFolder imapFolder = (IMAPFolder) store.getFolder(“Inbox”);
imapFolder.open(Folder.READ_ONLY);

final FlagTerm ft = this.getFlagTerm(new Flags(Flags.Flag.SEEN), false);
final Message[] messages = imapFolder.search(ft);

for (int i = 0; i < messages.length; i++) {
  final long imapUid = imapFolder.getUID(messages[i]);
  final MimeMessage message = new MimeMessage((MimeMessage) messages[i]);
}

easy… main problem is, that the MimeMessage is directly linked to the imap Folder… so if you return the MimeMessages and close the imapFolder… accessing the MimeMessage dont work… 🙁

Solution? CopyConstructor

MimeMessage copyedMessage = new MimeMessage(message);

nuff said!

mule with spring-beans and autowire…

… doesnt work 🙁

i really think (now partly thought) spring is cool… i used a spring-bean configfile, initialised all the entities and @Autowired them.. pretty cool… reflection on the entities to find and load them… and i think its really readable in java code… whenever you see an @Autowired… it will be created someware completely different and reflected to the position…

but autowiring with mule dont work… or i couldnt figure out how… mule runs its own context and the springcontext is somewhere in there… and the springcontext behaves different… crap… so new solution:

beans.xml

<bean id=”A” class=”ch.michio.spring.A”/>

<bean id=”B” class=”ch.michio.spring.B”>
     <property name=”a” ref=”A”/>
</bean>

so in class B there was an:

@Autowired
private A a;

 now there is a setter:

private A a;
public setA(A a){
     this.a = a;
}

So during initialisation

  1. an A object is created
  2. a B object is created
  3. a in spring bean container found A is set to B

my main problem with this solution is the readability… you need to read the java files AND the beans.xml… bah… 🙁

mule flow

needed to reconfigure our mule-config and i was really confused about the whole mule documentation… i really think its crap, but i figured it out 🙂

first a side note to the documentation: we use flows… and a co-worker told me, that flows are kinda new, so not everything might work… its kinda documented, but without any example… so hard to figure it out…

so what did i need? just a simple flow, which sends the result to the next flow… so simple outbound to inbound…

mule-config.xml
<flow name="fooWorklow">
<vm:inbound-endpoint ... />
  <component class="Foo" />
  <vm:outbound-endpoint path="myFooBar" exchange-pattern="one-way">
    <payload-type-filter expectedType="FoobarMessage" />
  </vm:outbound-endpoint>
</flow>

<flow name="barWorkflow">
  <vm:inbound-endpoint path="myFooBar" exchange-pattern="one-way" />
  <component class="Bar" />
</flow>

and some more… so the connection between these two workwlows is made by the path=”myFooBar”… in config… additionally the outbound payload-type-filter is a rooter, which defines, that only the class foobarMessage is allowed on this connection and only this class will be sent… other result will be burned (at the koax terminator)… so how do the classes look like?

Foo
public class Foo{
  public FoobarMessage inboundMethod(){
    ...
    return foobarMessage;
  }
}

Bar
public class Bar{
  public void process(FoobarMessage msg){
    ...
  }
}

and how does the message finds it method? Reflection… so just create one method with the message argument and voila… it finds it 🙂 pretty cool…

genericDAO

in my jpa project i needed DAOs… Data Access Objects… and the concept to implement those should be generic… initially this concept is confusing as hell… but after that its kinda cool 🙂

Each Entity needs an DAO and for every entity there are 6 object involved:

  • interfcae GenericDao<T, K>: the main Interface with the basic DAO functions (CRUD(create, read, update, delete))
  • class GenericDaoImpl<T, K>: implements the GenericDao interface.
  • interface <entity>Dao: interface of the entity DAO, implements the GenericDao<<entityType>, <entityPrimaryKeyType>>
  • class <entity>DaoImpl: implementation of the entity Dao. it is extended by the GenericDaoImpl
  • class <entity>: the entity class
  • class <entity>DaoTest: testing rocks 🙂

confused? ok heres the example: partly in pseudo code…

Basic things:
public interface GenericDao<T, K extends Serializable>{
T findById(K id, boolean lock);
List<T> findAll();
T save(T entity);
void delete(T entity);
}

public abstract class GenericDaoImpl<T, K extends Serializable> implements GenericDao<T, K>

public Person{
  int id;
  string name
}

Person Dao:
public interface PersionDao extends GenericDao<Persion, Integer>

public class PersonDaoImpl extends GenericDaoImpl<Person, Integer> implements PersionDao

And thats it… in the GenericDaoImpl are all the methods for all entites and in the PersonDaoImpl the persion stuff…

spring

in my project there are a lot of technologies involved and as a .net-developer everything is new… yeah… its java again 🙂 ok jpa, mule, spring, maven, pojos… and i finally understood the main concept of spring :)… or lets say the part with beans… ok i didnt understand, when (timewise) springbeans ware created… but i think this is kinda less important… ok its starts to be important, when something goes wrong, but can do…

 <bean id=”[objectId]” class=”[className]”>
<constructor-arg>
<value>[value]</value>
</constructor-arg>
</bean>

With this declaration an object of typ className will be generated with the name objectId. The constructor is filled with set values… the big question is: where the hell is this object? Actually i dont know, but it can be get in java with:

@Autowired
[className] [objectId];

done.

mule with spring and testing without mule…

mule is able to import spring beans… as expected 🙂 but to test those… its kinda hard… 🙁

first create an own bean config file separated from the mule-config.xml and include it in mule-config.xml with following tags:

<spring:beans>
<spring:import resource=”beans.xml”/>
</spring:beans>

simple 🙂 now the beans.xml is a simple springbeans config file… with <beans> as root tag and <bean>’s inside. easy too…

and now use following annotation in the unittest:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={“classpath:beans.xml”})

this should now load the bean correctly during test…

jpa cache

ok the entityManager in jpa caches… which is normally a good thing… but with OneToMany / ManyToOne mapping its kinda wired… my problem is, that i have an object A and an object B… A has one B… B has a List<A>…

  1. so created a B and persisted it…
  2. created a A with the B object and persited it
  3. checked A and has B 🙂
  4. checked B and has an empty list 🙁

and everything is in one transaction… unit test… if i persist it B with A in the list.. it works… but DB looks exactly the same… so there is a cache…

how can i kill it? or is it really a problem?

see: http://en.wikibooks.org/wiki/Java_Persistence/Caching

jpa mapping

java <-> oracle… with jpa sounds really easy, but was not… ok not really, because i just didnt get the whole thing… a problem was the naming… java class naming is different the database entities… so i needed to add mapping keywords…

@Entity
@Table (name=”<tablename>”, schema=”<schemaname>”)
public class <classname>{

@Id
@Column(name=”<colname>”)
private int id;

ok that was the simple part 🙂 the really hard part was the autoincrement… jpa allows different types of generatedValues and mainly it needs a specified sequence or trigger… the trigger is an absolute no go… and mapping sequence is wired… ok not soooo wired, but…

@Id
GeneratedValue(strategy = GenerationType.SEQUENCE, generator = “<sequenceName>”)
@SequenceGenerator(name = “<sequenceName>”, initialValue = 1, allocationSize = 1)
@Column(name=”<colname>”)
private int id;

the strategy needs an generator, which know the sequence and its behavior… and allocationSize is the increment value… which is 50 as default…