Bowly Posted February 16, 2016 Share Posted February 16, 2016 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? Quote Link to comment Share on other sites More sharing options...
JoNny Posted February 16, 2016 Share Posted February 16, 2016 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 Quote Link to comment Share on other sites More sharing options...
Bowly Posted February 16, 2016 Author Share Posted February 16, 2016 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. Quote Link to comment Share on other sites More sharing options...
Pepperonipizza Posted February 16, 2016 Share Posted February 16, 2016 Your error refers to lines 22, resp. line 33 in your MateriaalCatalogus.java file. Which lines are these? Quote Link to comment Share on other sites More sharing options...
Bowly Posted February 16, 2016 Author Share Posted February 16, 2016 Your error refers to lines 22, resp. line 33 in your MateriaalCatalogus.java file. Which lines are these? thats the em.getTransaction().commit(); Quote Link to comment Share on other sites More sharing options...
Pepperonipizza Posted February 16, 2016 Share Posted February 16, 2016 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. Quote Link to comment Share on other sites More sharing options...
Bowly Posted February 16, 2016 Author Share Posted February 16, 2016 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); } Quote Link to comment Share on other sites More sharing options...
Pepperonipizza Posted February 16, 2016 Share Posted February 16, 2016 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. Quote Link to comment Share on other sites More sharing options...
Bowly Posted February 16, 2016 Author Share Posted February 16, 2016 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 Quote Link to comment Share on other sites More sharing options...
Pepperonipizza Posted February 16, 2016 Share Posted February 16, 2016 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 ) 1 Quote Link to comment Share on other sites More sharing options...
Bowly Posted February 16, 2016 Author Share Posted February 16, 2016 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 ^^ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.