2

Im trying to do some example app with EJB, JPA-Hibernate.

I did a PostgreSQL database like this:

CREATE TABLE Klasa (
idKlasa SERIAL,
Profil VARCHAR NULL,
Skrot VARCHAR NULL,
PRIMARY KEY(idKlasa)
);

CREATE TABLE Przedmiot (
 idPrzedmiot SERIAL,
idKlasa INTEGER  NOT NULL,
Nazwa VARCHAR NULL,
PRIMARY KEY(idPrzedmiot),
FOREIGN KEY (idKlasa) REFERENCES Klasa(idKlasa)
);

CREATE TABLE Uczen (
 idUczen SERIAL,
 idKlasa INTEGER  NOT NULL,
Imie VARCHAR NULL,
 Nazwisko VARCHAR NULL,
PRIMARY KEY(idUczen),
FOREIGN KEY (idKlasa) REFERENCES Klasa(idKlasa)
);

CREATE TABLE Ocena (
idUczen INTEGER  NOT NULL,
idPrzedmiot INTEGER  NOT NULL,
Ocena INTEGER  NULL,
PRIMARY KEY(idUczen, idPrzedmiot),
FOREIGN KEY (idUczen) REFERENCES Uczen(idUczen),
FOREIGN KEY (idPrzedmiot) REFERENCES Przedmiot(idPrzedmiot)
 );

Then i generate Entities with EclipseLink 2.5.

Now, im trying to get any record from Klasa table. But i stuck on some errors while deploying on WildFly 8.2:

 20:38:11,089 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 50) MSC000001: Failed to start service jboss.persistenceunit."Lab1.war#Lab1": org.jboss.msc.service.StartException in service jboss.persistenceunit."Lab1.war#Lab1": javax.persistence.PersistenceException: [PersistenceUnit: Lab1] Unable to build Hibernate SessionFactory
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:172) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.8.0_40]
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:474)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_40]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_40]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_40]
at org.jboss.threads.JBossThread.run(JBossThread.java:122)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: Lab1] Unable to build Hibernate SessionFactory 

and:

20:38:11,089 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "Lab1.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"Lab1.war#Lab1\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"Lab1.war#Lab1\": javax.persistence.PersistenceException: [PersistenceUnit: Lab1] Unable to build Hibernate SessionFactory
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: Lab1] Unable to build Hibernate SessionFactory
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: pl.edu.wat.model.Ocena column: idprzedmiot (should be mapped with insert=\"false\" update=\"false\")"}}
20:38:11,089 WARN  [org.jboss.as.jpa] (Controller Boot Thread) JBAS011411: Unexpected problem gathering statistics: java.lang.IllegalStateException: JBAS011477: Persistence unit 'Lab1.war#Lab1' is not available

I have no idea what im doing wrong. I will post persistance.xml and Ocena Entity (becouse error point on it)

persistance.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="Lab1" transaction-type="JTA">
    <class>pl.edu.wat.model.Klasa</class>
    <class>pl.edu.wat.model.Ocena</class>
    <class>pl.edu.wat.model.OcenaPK</class>
    <class>pl.edu.wat.model.Przedmiot</class>
    <class>pl.edu.wat.model.Uczen</class>
    <properties>
        <property name="hibernate.connection.username" value="postgres" />
        <property name="hibernate.connection.password" value="postgres" />
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/Lab1" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />

    </properties>

</persistence-unit>
</persistence>

Ocena.java

@Entity
@NamedQuery(name="Ocena.findAll", query="SELECT o FROM Ocena o")
public class Ocena implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private OcenaPK id;

private Integer ocena;

//bi-directional many-to-one association to Przedmiot
@ManyToOne
@JoinColumn(name="idprzedmiot")
private Przedmiot przedmiot;

//bi-directional many-to-one association to Uczen
@ManyToOne
@JoinColumn(name="iduczen")
private Uczen uczen;

public Ocena() {
}

public OcenaPK getId() {
    return this.id;
}

public void setId(OcenaPK id) {
    this.id = id;
}

public Integer getOcena() {
    return this.ocena;
}

public void setOcena(Integer ocena) {
    this.ocena = ocena;
}

public Przedmiot getPrzedmiot() {
    return this.przedmiot;
}

public void setPrzedmiot(Przedmiot przedmiot) {
    this.przedmiot = przedmiot;
}

public Uczen getUczen() {
    return this.uczen;
}

public void setUczen(Uczen uczen) {
    this.uczen = uczen;
}

}

UPDATE

here are other ends of associations.

Uczen.java

@Entity
@NamedQuery(name="Uczen.findAll", query="SELECT u FROM Uczen u")
public class Uczen implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer iduczen;

private String imie;

private String nazwisko;

//bi-directional many-to-one association to Ocena
@OneToMany(mappedBy="uczen")
private List<Ocena> ocenas;

//bi-directional many-to-one association to Klasa
@ManyToOne
@JoinColumn(name="idklasa")
private Klasa klasa;

public Uczen() {
}

public Integer getIduczen() {
    return this.iduczen;
}

public void setIduczen(Integer iduczen) {
    this.iduczen = iduczen;
}

public String getImie() {
    return this.imie;
}

public void setImie(String imie) {
    this.imie = imie;
}

public String getNazwisko() {
    return this.nazwisko;
}

public void setNazwisko(String nazwisko) {
    this.nazwisko = nazwisko;
}

public List<Ocena> getOcenas() {
    return this.ocenas;
}

public void setOcenas(List<Ocena> ocenas) {
    this.ocenas = ocenas;
}

public Ocena addOcena(Ocena ocena) {
    getOcenas().add(ocena);
    ocena.setUczen(this);

    return ocena;
}

public Ocena removeOcena(Ocena ocena) {
    getOcenas().remove(ocena);
    ocena.setUczen(null);

    return ocena;
}

public Klasa getKlasa() {
    return this.klasa;
}

public void setKlasa(Klasa klasa) {
    this.klasa = klasa;
}

}

Przedmiot.java

@Entity
@NamedQuery(name="Przedmiot.findAll", query="SELECT p FROM Przedmiot p")
public class Przedmiot implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer idprzedmiot;

private String nazwa;

//bi-directional many-to-one association to Ocena
@OneToMany(mappedBy="przedmiot")
private List<Ocena> ocenas;

//bi-directional many-to-one association to Klasa
@ManyToOne
@JoinColumn(name="idklasa")
private Klasa klasa;

public Przedmiot() {
}

public Integer getIdprzedmiot() {
    return this.idprzedmiot;
}

public void setIdprzedmiot(Integer idprzedmiot) {
    this.idprzedmiot = idprzedmiot;
}

public String getNazwa() {
    return this.nazwa;
}

public void setNazwa(String nazwa) {
    this.nazwa = nazwa;
}

public List<Ocena> getOcenas() {
    return this.ocenas;
}

public void setOcenas(List<Ocena> ocenas) {
    this.ocenas = ocenas;
}

public Ocena addOcena(Ocena ocena) {
    getOcenas().add(ocena);
    ocena.setPrzedmiot(this);

    return ocena;
}

public Ocena removeOcena(Ocena ocena) {
    getOcenas().remove(ocena);
    ocena.setPrzedmiot(null);

    return ocena;
}

public Klasa getKlasa() {
    return this.klasa;
}

public void setKlasa(Klasa klasa) {
    this.klasa = klasa;
}

}
countryroadscat
  • 1,660
  • 4
  • 26
  • 49

1 Answers1

0

You didn't show us the other ends of the bidirectional associations of your Ocena, and I suspect that's where the problem is.

For bidirectional associations, one end is the owner, and the other end must use the mappedBy attribute in the mapping annotation.

Update: The mappedBy attributes look OK. What about your OcenaPK class? Does it contain the duplicate mapping for the idPrzedmiot column?

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63