Thursday, June 30, 2011

eclipse replace multiple lines


Problem: Hibernate Tools DAO generation generates SeeionFactory with getter via JNDI

private final SessionFactory sessionFactory = getSessionFactory();


protected SessionFactory getSessionFactory() {

try {

return (SessionFactory) new InitialContext()

.lookup("SessionFactory");

} catch (Exception e) {

log.error("Could not locate SessionFactory in JNDI", e);

throw new IllegalStateException(

"Could not locate SessionFactory in JNDI");

}

}


while I need the regular java bean getter setter without JNDI context

private SessionFactory sessionFactory;


public SessionFactory getSessionFactory() {

return sessionFactory;

}


public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}


Solution: use Eclipse search+replace function, on the GUI, looks like it supports only 1 single line replacement, in fact it isn't, but multiple lines cannot be entered at the search field using the keyboard's Enter key, it requires using copy+paste


Tuesday, June 14, 2011

java.net.BindException: Permission denied on Mac OSX

problem:
running any java app that listens to a port

Caused by: java.net.BindException: Permission denied

at java.net.PlainSocketImpl.socketBind(Native Method)

at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)

at java.net.ServerSocket.bind(ServerSocket.java:328)

at java.net.ServerSocket.(ServerSocket.java:194)

at javax.net.DefaultServerSocketFactory.createServerSocket(ServerSocketFactory.java:170)

at org.apache.activemq.transport.tcp.TcpTransportServer.bind(TcpTransportServer.java:135)

... 44 more


solution:
1. use sudo
or
2. listen to a port that is outside of 0 to 1023

Thursday, June 9, 2011

Hibernate + PostgreSQL + Table Partitioning

Problem: using Hibernate to insert a new entry into a partitioned table on PostgreSQL would get the following exception:


2011-06-08 16:37:07,725 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] -

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1


Solution:

use a @SQLInsert(sql = "insert into table (col_1, col_2) values (?, ?)", check=ResultCheckStyle.NONE)


more solutions provided here:

http://www.redhat.com/f/pdf/jbw/jmlodgenski_940_scaling_hibernate.pdf

Wednesday, June 8, 2011

Hibernate (Annotation) + PostgreSQL + Sequence Generator

Problem: The java class generated by Hibernate Code Generator does not know anything about the sequence defined on PostgreSQL.

Solution: use the combination of @javax.persistence.GeneratedValue & @org.hibernate.annotation.GenericGenerator

@Id

@Column(name = "id", unique = true, nullable = false, insertable=false)

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "IdSeqGeneratorName")

@org.hibernate.annotations.GenericGenerator(name = "IdSeqGeneratorName", strategy = "sequence", parameters = { @Parameter(name = "sequence", value = "the_postgres_seq") })

public long getId() {

return this.id;

}



or

@Id

@Column(name = "id", unique = true, nullable = false, insertable=false)

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tcp_audit_seq_name")

@SequenceGenerator(name = "tcp_audit_seq_name", sequenceName = "tcp_audit_seq", allocationSize = 1)

public long getId() {

return this.id;

}