Reputation: 42444
I use netbean to create java EE application and two projects are created automatically, one is XXXXX-war while the other is XXXXX-ejb. What is the responsibility of these two projects? I create a few java entity classes in ejb project and place the persistence-unit and glassfish-resoruces.xml there. Then create some managed bean classes, *.xhtml in war project. Then I create a derby database. Is this the correct way to do that?
I got below exception when deploy war project on glassfish server.
Invalid resource : jdbc/<databasename>__pm
I have searched some related problems in google but none of them provide a solution. Then I move the persistence-unit and glashfish-resources.xml files to be war project. After doing this, I am able to deploy war project without such error. I wander what is the right way to put these resources file? And why do we need two projects (ejb and war) in a Java EE application.
Below is glassfish-resources.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.apache.derby.jdbc.ClientDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="derby_net_GovernmentDB_governmentPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="1527"/>
<property name="databaseName" value="GovernmentDB"/>
<property name="User" value="government"/>
<property name="Password" value="government"/>
<property name="URL" value="jdbc:derby://localhost:1527/GovernmentDB"/>
<property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="java:app/warjndi" object-type="user" pool-name="derby_net_GovernmentDB_governmentPool"/>
</resources>
Below is persisence-unit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="GovernmentDepartment-warPU" transaction-type="JTA">
<jta-data-source>java:app/warjndi</jta-data-source>
<class>homesoft.govDept.entity.SystemUserEntity</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Additional question, with the above configuration, after deploying my war project, I can see a table(SEQUENCE) is created automatically under the database GovernmentDB. But I don't have any entity class named SEQUENCE. Where is this table from? My entity class is defined in ejb project like below:
package homesoft.govDept.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author Kaley
*/
@Entity
@Table(name="system_user")
public class SystemUserEntity implements Serializable {
private long id;
private String lastName;
private String firstName;
private String email;
private String password;
private String type;
private String address;
private String phoneNumber;
@GeneratedValue
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Upvotes: 1
Views: 95
Reputation: 9857
Before Java EE 6, an ejb couldn't be deployed on a war file, so having a separate .jar containing all the ejbs included in .ear was mandatory.
It is likely that netbeans still use this "old" approach. Try to have a look at project creation time, netbeans would probably ask what type of project you need.
Regarding the extra SEQUENCE table, you have specified @GeneratedValue
for your @Id
which means the id is being generated with the default strategy AUTO, which in turns delegate this to the underlyng DB.
It sounds strange to me but if you haven't created the table yourself, it is likely that derbyDB manage the identity with a table holding all incremented value for entities, infact in your persistence-unit.xml you have declared to generate the schema, which results in creating all tables at startup:
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
If you introspect the SEQUENCE table you should find some reference to all entities declared with @GeneratedValue
.
Upvotes: 1