Home
- Details
The problem: if you want to avoid overengineering with DTO classes and use the entities in jsf, then you can get a problem with select boxes and transformation for @OneToMany and @ManyToOne relationships.
The Error message is something like : "' conversion error ... null Converter"
Example (with Java EE 7 and JSF 2.2):
1. Class Game:
public class Game {
@ManyToOne
private Team team1;
...
}
2. Class Team:
public class Team {
private String name;
...
}
3. createGame.xhtml with select box for choosing a first Team:
<h:selectOneListbox id="team1" value="#{gamesBean.createdGame.team1}" converter="#{teamsConverter}">
<f:selectItems value="#{teamsBean.teams}" var="t" itemLabel="#{t.name}" itemValue="#{t}" />
</h:selectOneListbox>
4. TeamsConverter with implemented two methods from Converter interface:
import javax.faces.convert.Converter;
@ManagedBean(name = "teamsConverter")
@FacesConverter(forClass = Game.class)
public class TeamsConverter implements Converter, Serializable {
@Inject
private TeamService teamService;
public TeamsConverter() {
}
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
if (value.isEmpty()) {
return null;
}
Long id = new Long(value);
return teamService.findById(id);
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null || value.toString().isEmpty()) {
return "";
}
Team t = (Team) value;
Long id = t.getId();
return id.toString();
}
}
5. The Class Team should overwride hashCode, equals and toString
@Override
public String toString() {
return "de.lustin.flc.domain.Team[ id=" + id + " ]";
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Team)) {
return false;
}
Team other = (Team) object;
if ((this.id == null && other.id != null)
|| (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
Yout can download the complete example from github:
- Details
1. Add User to WildFly for JDBC Datasource configuration
- go to {wildfly-8.2.0}\bin
- run add-users.bat
- follow the instructions for cuser creating
2. Add MySQL Driver to WildFly
- Download the mysql-driver mysql-connector-java-{last_version}.jar from any Maven Repository e.g.: http://repo1.maven.org/maven2/mysql/mysql-connector-java/
- go to WildFly modules Directory:
cd {wildfly-8.2.0}/modules/system/layers/base
mkdir -p mysql/mysql-connector-java/maincopy mysql-driver mysql-connector-java-{last_version}.jar here.
- add a file module.xml with content:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1"
name="mysql.mysql-connector-java">
<resources>
<resource-root path="mysql-connector-java-5.1.22.jar"/>
<!-- Insert resources here -->
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module> -
Add Driver in standalone.xml
cd {wildfly-8.2.0}/standalone/configuration/
edit standalone.xmladd to drivers:
<driver name="com.mysql" module="mysql.mysql-connector-java">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
3. Add Datasource to WildFly
- open http://localhost:9990/console/App.htm
- go to Connector->Datasources
- add new Datasource
- Give the name and JNDI name.
- choose your created driver
- enter your connection URL like: jdbc:mysql://localhost/mydatabase and database cridentials
- test your connection
- Details
-
Go to the GlassFish administration console
-
open: Configurations -> server-config -> JVM Settings
-
Check Debug enabled
-
Save changes
-
Restart GlassFish
- Details
1. Add joda-time maven dependency to your Java 1.7 Project
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
2. Format date string and use LocalDate and Years classes for age calculating:
import org.joda.time.LocalDate;
import org.joda.time.Years;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
....
String dateString = "12.05.1990";
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd.MM.yyyy");
LocalDate dateOfBirth = dtf.parseLocalDate(dateString);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(dateOfBirth, now);
System.out.println(age.getYears());
- Details
1. Run mysql
2. execute source <sql_file>:
mysql> source /home/sqlskripts/mydata.sql
Seite 5 von 10