У меня проблема с созданием простой базы данных с EJB. Я создал веб-проект в Netbeans с Maven. Я использую Glassfish 5, но проблема также возникает с Glassfish 4.
Вот мои уроки.
Ui_model.java:
@Named("model")
@Stateless
public class Ui_model implements Serializable{
@Inject
PizzaRepository pizzas;
public Ui_model() {
}
@PostConstruct
public void init(){
Pizza one = new Pizza(1, "Pizza Margaritta", 4.5);
Pizza two = new Pizza(2, "Pizza Napoli", 5);
Pizza three = new Pizza(3, "Pizza Calzone", 6);
pizzas.createDB(one);
pizzas.createDB(two);
pizzas.createDB(three);
}
}
PizzaRepository.java:
@Named
@ApplicationScoped
public class PizzaRepository implements Serializable{
@PersistenceContext(unitName="Unit")
protected EntityManager em;
public void createDB(Pizza pizza){
try{
em.persist(pizza);
}catch(RollbackException e){
System.out.println(e.getMessage());
}
}
}
Pizza.java:
@Entity
public class Pizza implements Serializable{
@Id
int number;
String name;
double price;
public Pizza(int number, String name, double price) {
this.number = number;
this.name = name;
this.price = price;
}
public Pizza(){
}
}
Это мой файл persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Unit" transaction-type="JTA">
<jta-data-source>java:app/jdbc/testdb</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- database connection -->
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
</persistence-unit>
</persistence>
Это мой glassfish-resources.xml:
<?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_testdb_usernamePool"
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="testdb"/>
<property name="User" value="username"/>
<property name="Password" value="123456"/>
<property name="URL" value="jdbc:derby://localhost:1527/testdb"/>
<property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true"
jndi-name="java:app/jdbc/testdb"
object-type="user"
pool-name="java:app/derby_net_testdb_usernamePool"/>
</resources>
Структура моего проекта: ![My project structure:](https://i.stack.imgur.com/Ul46G.jpg)
При запуске я получаю следующую ошибку:
![enter image description here](https://i.stack.imgur.com/cfocW.jpg)
Странно то, что в тестовой базе данных по-прежнему создается пустая таблица с именем Pizza, но без записей. Так что это не значит, что программа не находит саму БД. Я действительно не знаю, что делать с этой ошибкой. Первоначально проект был намного больше, но я разбил его, чтобы решить основную проблему, и это не намного легче, чем это. Тем не менее это не работает. Буду признателен за любой совет, как это исправить.
Редактировать: приглашения asadmin list-jndi-записи:
java:global: com.sun.enterprise.naming.impl.TransientContext
UserTransaction: com.sun.enterprise.transaction.startup.TransactionLifecycleService$2
__internal_java_app_for_app_client__testdb__java:app: com.sun.enterprise.naming.impl.TransientContext
concurrent: com.sun.enterprise.naming.impl.TransientContext
com.sun.enterprise.container.common.spi.util.InjectionManager: com.sun.enterprise.container.common.impl.util.InjectionManagerImpl
jms: com.sun.enterprise.naming.impl.TransientContext
ejb: com.sun.enterprise.naming.impl.TransientContext
jdbc: com.sun.enterprise.naming.impl.TransientContext
Command list-jndi-entries executed successfully.
asadmin list-jndi-записи --context jdbc приглашения:
__default: org.glassfish.resourcebase.resources.api.ResourceProxy
__TimerPool: org.glassfish.resourcebase.resources.api.ResourceProxy
sample: org.glassfish.resourcebase.resources.api.ResourceProxy
Command list-jndi-entries executed successfully.