Jump to content
Bowly

Help needed with JAVA(javafx with JPA)

Recommended Posts

so i'm working on a project for school and im having problems with javafx and jpa. anyone here familiar with both? 

 

Errormessage: https://gyazo.com/dc6df9873543668e4aba42d69d591774

 

i'm trying to add data into the database, all type are SimpleStringProperty, SimpleIntegerProperty and SimpleBooleanProperty since we're using lists in our gui.

but these gave problems with database, so i used accesstype on property on getters which solved alot of problems. But now getting this error, i dont really know what to do since everything does get added into the database. 

 

Any ideas? 

Share this post


Link to post
Share on other sites

so i'm working on a project for school and im having problems with javafx and jpa. anyone here familiar with both? 

 

Errormessage: https://gyazo.com/dc6df9873543668e4aba42d69d591774

 

i'm trying to add data into the database, all type are SimpleStringProperty, SimpleIntegerProperty and SimpleBooleanProperty since we're using lists in our gui.

but these gave problems with database, so i used accesstype on property on getters which solved alot of problems. But now getting this error, i dont really know what to do since everything does get added into the database. 

 

Any ideas? 

 

The relevant code snippet would be cool to have ;)

 

/JoNny

Share this post


Link to post
Share on other sites

The relevant code snippet would be cool to have ;)

 

/JoNny

 

This is the class i'm trying to map to the database, left out unrelevent methodes.

 

 

@Entity

@Table(name = "materiaal")
public class Materiaal {
 
    private Long id;
 
    private String foto;
    private SimpleStringProperty naam;
    private SimpleStringProperty omschrijving;
    private SimpleIntegerProperty artikelnummerFirma;
    private SimpleDoubleProperty prijs;
    private SimpleIntegerProperty aantal;
    private SimpleIntegerProperty aantalBeschikbaar;
    private SimpleBooleanProperty uitleenbaarheid;
    private SimpleStringProperty plaats;
 
    private Set<Firma> firmas;
    private Set<Doelgroep> doelgroepen;
    private Set<Leergebied> leergebieden;
 
    public Materiaal(String naam, int aantal) {
        this.naam = new SimpleStringProperty();
        this.aantal = new SimpleIntegerProperty();
        setNaam(naam);
        setAantal(aantal);
        setOmschrijving(null);
        setArtikelnummerFirma(0);
        setPlaats(null);
        setPrijs(0);
        setAantalBeschikbaar(0);
        setUitleenbaarheid(false);
    }
 
    protected Materiaal() {
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }
 
    @Access(AccessType.PROPERTY)
    public String getNaam() {
        return naam.get();
    }
 
    public SimpleStringProperty geefNaam() {
        return naam;
    }
 
    public void setNaam(String naam) {
        this.naam.set(naam);
 
    }
 
    public SimpleIntegerProperty geefAantal() {
        return aantal;
    }
 
    @Access(AccessType.PROPERTY)
    public int getAantal() {
 
        return aantal.get();
    }
 
    public void setAantal(int aantal) {
        this.aantal.set(aantal);
    }
 
 
    }

 
this is the class where i put it in the database
 

public class MateriaalCatalogus extends Observable{
 
    private ObservableList<Materiaal> observableMaterialen;
    private List<Materiaal> materialen=new ArrayList<>(Arrays.asList(new Materiaal[]{
        new Materiaal("test", 5),
        new Materiaal("test2", 3)}));
 
    public MateriaalCatalogus() {
        observableMaterialen = FXCollections.observableArrayList(materialen);
    }
 
    /**
     *
     * @param materiaal
     */
    public void materiaalToevoegen(Materiaal materiaal) {
        observableMaterialen.add(materiaal);
        EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
        em.getTransaction().begin();
        em.persist(materiaal);
        em.getTransaction().commit();
        em.close();
        JPAUtil.getEntityManagerFactory().close();
    }

 
dont mind the hardcoded things, those are just to test wether the list in gui works.

Share this post


Link to post
Share on other sites

Your error refers to lines 22, resp. line 33 in your MateriaalCatalogus.java file. Which lines are these?

 

thats the em.getTransaction().commit();

Share this post


Link to post
Share on other sites

This code is fishy, but I assume that you're doing it because you're hardcoding some stuff:

    private ObservableList<Materiaal> observableMaterialen;
    private List<Materiaal> materialen=new ArrayList<>(Arrays.asList(new Materiaal[]{
        new Materiaal("test", 5),
        new Materiaal("test2", 3)}));
 
    public MateriaalCatalogus() {
        observableMaterialen = FXCollections.observableArrayList(materialen);
    }

You're getting an error because your commit() doesn't succeed.

Your commit didn't succeed because (and this is mostly a guess) you're calling setAantal(..) on an invalid object.

 

You're using an aweful lot of extra libraries I'm not familiar with, so my best advice would be to look closely at how you're doing your setAantal stuff.

Share this post


Link to post
Share on other sites

well i've been searching on this for over 8 hours now, and maybe my brain is going derp but i fail to find something wrong (also this is the first time using javafx together with jpa)

 

this is the code where i create the object i will persist in materiaalCatalogus.

 

 

 

@FXML
    private void addNieuwMateriaal(ActionEvent event) {
        Materiaal materiaal = new Materiaal(txfArtikelNaam.getText(), Integer.parseInt(txfAantal.getText()));
        materiaal.setArtikelnummerFirma(Integer.parseInt(txfArtikelNummer.getText()));
        materiaal.setOmschrijving(txaOmschrijving.getText());
        materiaal.setPrijs(Double.parseDouble(txfPrijs.getText()));
        materiaal.setPlaats(txfPlaats.getText());
        if (cbUitleenbaar.isSelected()) {
            materiaal.setUitleenbaarheid(true);
        }
        clearText();
        materiaalController.materiaalToevoegen(materiaal);
    }

 

 

Share this post


Link to post
Share on other sites
 
        Materiaal materiaal = new Materiaal(txfArtikelNaam.getText(), Integer.parseInt(txfAantal.getText()));

Replace this with


        Materiaal materiaal = new Materiaal(txfArtikelNaam.getText(), HARDCODED_INTEGER);
where HARDCODED_INTEGER is something you replace with a value Integer (I think?) value.
 
If this fixes the problem, your text input is somehow not valid.

Share this post


Link to post
Share on other sites

 

 
        Materiaal materiaal = new Materiaal(txfArtikelNaam.getText(), Integer.parseInt(txfAantal.getText()));

Replace this with


        Materiaal materiaal = new Materiaal(txfArtikelNaam.getText(), HARDCODED_INTEGER);
where HARDCODED_INTEGER is something you replace with a value Integer (I think?) value.
 
If this fixes the problem, your text input is somehow not valid.

 

 

still same problem

Share this post


Link to post
Share on other sites

Let the code rest for a while then. Go to sleep (I know you're in the same timezone as me ;-)) and look at it again in a day or 2-3 from now.

 

It's not a guarantee, but it's something that I've found working very well for me when I was debugging.

 

 

(This is assuming you don't have a deadline soon :P)

  • Like 1

Share this post


Link to post
Share on other sites

Ok so i have finally found my problem, apperently instatiating the simpleXproperty in the declaration at the top resolves this problem. i have no idea why, but apperently it does.

not sure if this allowed tho x)

 

But thanks alot for the help.

 

and the deadline for a working demo is thursday ^^

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

About Us

We are glad you decided to stop by our website and servers. At Fearless Assassins Gaming Community (=F|A=) we strive to bring you the best gaming experience possible. With helpful admins, custom maps and good server regulars your gaming experience should be grand! We love to have fun by playing online games especially W:ET, Call of Duty Series, Counter Strike: Series, Minecraft, Insurgency, DOI, TF2 & Battlefield Series and if you like to do same then join us! Here, you can make worldwide friends while enjoying the game. Anyone from any race and country speaking any language can join our Discord and gaming servers. We have clan members from US, Canada, Europe, Sri Lanka, India, Japan, Australia, Brazil, UK, Austria, Poland, Finland and many other countries. It doesn't matter how much good you are in the game or how much good English you speak. We believe in making new friends from all over the world. If you want to have fun and want to make new friends join up our gaming servers and our VoIP servers any day and at any time. .

×

Important Information

By using this site, you agree to our Terms of Use.