Hibernate Problem

  • Themenstarter Themenstarter henie
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
H

henie

Gast
Hallo,

ich bin Hibernate-Anfänger, brauche es aber relativ dringend und bin mit einem einfachen Beispiel angefangen.
Ich habe 2 Tabellen, Tisch und Stuhl zwischen denen einen 1:n Beziehung besteht.
tabellena4f84ea6JPG.jpg




Die Tabellen hab ich in MySQL angelegt und versuche nun etwas darin zu speichern, was allerdings
nicht funktioniert und ich weiß nicht warum das so ist.

Ausschnitt aus meiner Klasse Stuhl:
Code:
@Entity
@Table(name="stuhl")
public class Stuhl implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@SequenceGenerator(name = "stuhl_gen", sequenceName = "stuhl_id_seq")
	
	private Integer id;
	
	@Column(name="Material")
	private String Material;
	
	@ManyToOne
	@JoinColumn(name="tisch_id")
	private Tisch tisch;

aus Klasse Tisch:
Code:
@Entity
@Table(name="tisch")
public class Tisch implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@SequenceGenerator(name = "tisch_gen", sequenceName = "tisch_id_seq")
	private Integer id;

	@Column(name="Fuesse")
	private Integer Fuesse;

	@Column(name="Material")
	private String Material;
	
	@OneToMany(mappedBy="tisch")
	private Set<Stuhl> stühle = new HashSet<Stuhl>();

mein Code zum anlegen von einem Tisch und 2 Stühlen
Code:
  Tisch tisch1=new Tisch();
	   tisch1.setFuesse(4);
           tisch1.setMaterial("Eiche");	    
	    ses.save(tisch1);
		
	    Stuhl stuhl1=new Stuhl();
	    stuhl1.setMaterial("Buche");
	    stuhl1.setTisch(tisch1);
	    ses.save(stuhl1);
	    tisch1.getStühle().add(stuhl1);
    
	    Stuhl stuhl2=new Stuhl();
	    stuhl2.setMaterial("Eiche");	
	    stuhl2.setTisch(tisch1);
	    ses.save(stuhl2);
	    tisch1.getStühle().add(stuhl2);
	    
	    tx.commit();

und dann die auftretende Fehlermeldung
Code:
16:52:43,209  WARN JDBCExceptionReporter:100 - SQL Error: 1452, SQLState: 23000
16:52:43,209 ERROR JDBCExceptionReporter:101 - Cannot add or update a child row: 
a foreign key constraint fails (`beispiel/stuhl`, CONSTRAINT `tisch_id` FOREIGN KEY (`id`) 
REFERENCES `tisch` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: 
could not insert: [obj.Stuhl]
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)....
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: 
Cannot add or update a child row: a foreign key constraint fails (`beispiel/stuhl`, 
CONSTRAINT `tisch_id` FOREIGN KEY (`id`) REFERENCES `tisch` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)...

Könnt Ihr mir da weiterhelfen?
 
Ich vermute, es liegt daran, dass Du einen Stuhl speichern willst, bevor der Stuhl beim Tisch gesetzt ist. Speicher doch Tisch und Stühle mal am Ende der Methode (aber vor dem commit).
 
Ich hab das jetzt mal probiert, es tritt aber nach wie vor derselbe Fehler auf.
 
Kannst Du eventuell mal show_sql einschalten und dann schauen, in welcher Reihenfolge die SQL-Befehle reinkommen?
 
Code:
16:52:41,010  INFO Version:15 - Hibernate Annotations 3.4.0.GA
16:52:41,040  INFO Environment:543 - Hibernate 3.3.1.GA
16:52:41,040  INFO Environment:576 - hibernate.properties not found
16:52:41,040  INFO Environment:709 - Bytecode provider name : javassist
16:52:41,050  INFO Environment:627 - using JDK 1.4 java.sql.Timestamp handling
16:52:41,140  INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA
16:52:41,140  INFO Configuration:1460 - configuring from resource: /hibernate.cfg.xml
16:52:41,140  INFO Configuration:1437 - Configuration resource: /hibernate.cfg.xml
16:52:41,412  INFO Configuration:1575 - Configured SessionFactory: null
16:52:41,412  INFO HibernateSearchEventListenerRegister:53 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
16:52:41,512  INFO AnnotationBinder:419 - Binding entity from annotated class: obj.Tisch
16:52:41,592  INFO EntityBinder:422 - Bind entity obj.Tisch on table Tisch
16:52:41,723  INFO AnnotationBinder:419 - Binding entity from annotated class: obj.Stuhl
16:52:41,723  INFO EntityBinder:422 - Bind entity obj.Stuhl on table Stuhl
16:52:41,873  INFO CollectionBinder:650 - Mapping collection: obj.Tisch.stühle -> Stuhl
16:52:41,873  INFO AnnotationConfiguration:369 - Hibernate Validator not found: ignoring
16:52:41,883  INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
16:52:41,883  INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
16:52:41,883  INFO DriverManagerConnectionProvider:68 - autocommit mode: false
16:52:41,903  INFO DriverManagerConnectionProvider:103 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/beispiel
16:52:41,903  INFO DriverManagerConnectionProvider:109 - connection properties: {user=root, password=****}
16:52:42,215  INFO SettingsFactory:116 - RDBMS: MySQL, version: 5.0.67-community-nt
16:52:42,215  INFO SettingsFactory:117 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.8 ( Revision: ${svn.Revision} )
16:52:42,245  INFO Dialect:175 - Using dialect: org.hibernate.dialect.MySQLDialect
16:52:42,245  INFO TransactionFactoryFactory:62 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
16:52:42,245  INFO TransactionManagerLookupFactory:80 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
16:52:42,255  INFO SettingsFactory:170 - Automatic flush during beforeCompletion(): disabled
16:52:42,255  INFO SettingsFactory:174 - Automatic session close at end of transaction: enabled
16:52:42,255  INFO SettingsFactory:181 - JDBC batch size: 15
16:52:42,255  INFO SettingsFactory:184 - JDBC batch updates for versioned data: disabled
16:52:42,255  INFO SettingsFactory:189 - Scrollable result sets: enabled
16:52:42,255  INFO SettingsFactory:197 - JDBC3 getGeneratedKeys(): enabled
16:52:42,255  INFO SettingsFactory:205 - Connection release mode: auto
16:52:42,255  INFO SettingsFactory:229 - Maximum outer join fetch depth: 2
16:52:42,255  INFO SettingsFactory:232 - Default batch fetch size: 1
16:52:42,255  INFO SettingsFactory:236 - Generate SQL with comments: disabled
16:52:42,255  INFO SettingsFactory:240 - Order SQL updates by primary key: disabled
16:52:42,255  INFO SettingsFactory:244 - Order SQL inserts for batching: disabled
16:52:42,255  INFO SettingsFactory:420 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:52:42,265  INFO ASTQueryTranslatorFactory:47 - Using ASTQueryTranslatorFactory
16:52:42,265  INFO SettingsFactory:252 - Query language substitutions: {}
16:52:42,265  INFO SettingsFactory:257 - JPA-QL strict compliance: disabled
16:52:42,265  INFO SettingsFactory:262 - Second-level cache: enabled
16:52:42,265  INFO SettingsFactory:266 - Query cache: disabled
16:52:42,265  INFO SettingsFactory:405 - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
16:52:42,265  INFO SettingsFactory:276 - Optimize cache for minimal puts: disabled
16:52:42,265  INFO SettingsFactory:285 - Structured second-level cache entries: disabled
16:52:42,275  INFO SettingsFactory:305 - Echoing all SQL to stdout
16:52:42,275  INFO SettingsFactory:314 - Statistics: disabled
16:52:42,275  INFO SettingsFactory:318 - Deleted entity synthetic identifier rollback: disabled
16:52:42,275  INFO SettingsFactory:333 - Default entity-mode: pojo
16:52:42,275  INFO SettingsFactory:337 - Named query checking : enabled
16:52:42,365  INFO SessionFactoryImpl:187 - building session factory
16:52:42,656  INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured
16:52:42,656  INFO SchemaUpdate:155 - Running hbm2ddl schema update
16:52:42,656  INFO SchemaUpdate:167 - fetching database metadata
16:52:42,666  INFO SchemaUpdate:179 - updating schema
16:52:42,707  INFO TableMetadata:62 - table found: beispiel.stuhl
16:52:42,707  INFO TableMetadata:63 - columns: [id, tisch_id, material]
16:52:42,707  INFO TableMetadata:65 - foreign keys: [tisch_id]
16:52:42,707  INFO TableMetadata:66 - indexes: [primary, tisch_id]
16:52:42,727  INFO TableMetadata:62 - table found: beispiel.tisch
16:52:42,727  INFO TableMetadata:63 - columns: [id, fuesse, material]
16:52:42,727  INFO TableMetadata:65 - foreign keys: []
16:52:42,727  INFO TableMetadata:66 - indexes: [primary]
16:52:43,008  INFO SchemaUpdate:217 - schema update complete
16:52:43,178 DEBUG SQL:111 - insert into Tisch (Fuesse, Material) values (?, ?)
Hibernate: insert into Tisch (Fuesse, Material) values (?, ?)
16:52:43,188 DEBUG SQL:111 - insert into Stuhl (Material, tisch_id) values (?, ?)
Hibernate: insert into Stuhl (Material, tisch_id) values (?, ?)
16:52:43,199 DEBUG SQL:111 - insert into Stuhl (Material, tisch_id) values (?, ?)
Hibernate: insert into Stuhl (Material, tisch_id) values (?, ?)
16:52:43,209  WARN JDBCExceptionReporter:100 - SQL Error: 1452, SQLState: 23000
16:52:43,209 ERROR JDBCExceptionReporter:101 - Cannot add or update a child row: a foreign key constraint fails (`beispiel/stuhl`, CONSTRAINT `tisch_id` FOREIGN KEY (`id`) REFERENCES `tisch` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [obj.Stuhl]...
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben