Results 1 to 4 of 4

Thread: 1.2M1 pizzashop sample script and MySQL

  1. #1
    Join Date
    Aug 2010
    Location
    Goteborg, Sweden
    Posts
    434

    Default 1.2M1 pizzashop sample script and MySQL

    Changing the JPA setup for the generated project from the pizzashop sample project script with "jpa setup --provider HIBERNATE --database MYSQL --databaseName pizzashop --userName sa", fails with
    [main] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExc eptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in file [/Users/mik/Documents/Kodkrigaren/Java/STS-2.7.1/spring-samples/pizzashop/target/classes/META-INF/spring/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Foreign key (FKB7A02153835636DE: pizza_order_pizzas [pizza_order])) must have same number of columns as the referenced primary key (pizza_order [shop_city,shop_country,shop_name])
    I suppose something in the set up is not suited to MySQL, but I haven't identified it yet. Any ideas?

    Roo1.2M1, jdk 1.5, Mac OS X 10.5.8

  2. #2
    Join Date
    Aug 2010
    Location
    Goteborg, Sweden
    Posts
    434

    Default

    Interestingly using "jpa setup --provider ECLIPSELINK --database MYSQL --databaseName pizzashop --userName sa" works. So this seems to be something unique to Hibernate.
    To make sure I switched database again:"jpa setup --provider HIBERNATE --database H2_IN_MEMORY ". The same error as before.
    I'm investigating Hibernate and composite primary keys, suspecting this is a known problem.

  3. #3
    Join Date
    Aug 2010
    Location
    Goteborg, Sweden
    Posts
    434

    Default

    I found this section in the Hibernate manual to be interesting: 5.1.2.1. Composite identifier.

  4. #4
    Join Date
    Aug 2010
    Location
    Goteborg, Sweden
    Posts
    434

    Lightbulb

    As can be read in the previous link to the Hibernate manual there seems to be a handful of solutions for using composite keys in Hibernate. I have a such a business case coming up, but I'm leaning on making the composite keys to be just entity attributes and not rely on these for relations.
    For the pizzashop project I decided it was not interesting to really solve at the moment, so I adjusted the roo script for Hibernate and MySQL, created a database in MySQL with name "pizzashop" and granted user "sa" the appropriate permissions and then removed the composite key parts from the original pizzashop.roo script (changed lines in bold):
    Code:
    // Create a new project
    project com.springsource.pizzashop
    
    // Setup JPA persistence using Hibernate and MySQL
    jpa setup --provider HIBERNATE --database MYSQL --databaseName pizzashop --userName sa
    
    // Create domain entities
    entity --class ~.domain.Base --activeRecord false --testAutomatically
    field string --fieldName name --sizeMin 2 --notNull
    
    entity --class ~.domain.Topping --activeRecord false --testAutomatically
    field string --fieldName name --sizeMin 2 --notNull
    
    entity --class ~.domain.Pizza --activeRecord false --testAutomatically
    field string --fieldName name --notNull --sizeMin 2
    field number --fieldName price --type java.math.BigDecimal
    field set --fieldName toppings --type ~.domain.Topping
    field reference --fieldName base --type ~.domain.Base
    
    entity --class ~.domain.PizzaOrder --testAutomatically --activeRecord false
    field string --fieldName name --notNull --sizeMin 2
    field string --fieldName address --sizeMax 30
    field number --fieldName total --type java.math.BigDecimal
    field date --fieldName deliveryDate --type java.util.Date
    field set --fieldName pizzas --type ~.domain.Pizza
    
    field string --fieldName shopCountry 
    field string --fieldName shopCity
    field string --fieldName shopName
    
    // Define a repository layer for persistence
    repository jpa --interface ~.repository.ToppingRepository --entity ~.domain.Topping
    repository jpa --interface ~.repository.BaseRepository --entity ~.domain.Base
    repository jpa --interface ~.repository.PizzaRepository --entity ~.domain.Pizza
    repository jpa --interface ~.repository.PizzaOrderRepository --entity ~.domain.PizzaOrder
    
    // Define a service/facade layer
    service --interface ~.service.ToppingService --entity ~.domain.Topping
    service --interface ~.service.BaseService --entity ~.domain.Base
    service --interface ~.service.PizzaService --entity ~.domain.Pizza
    service --interface ~.service.PizzaOrderService --entity ~.domain.PizzaOrder
    
    // Offer JSON remoting for all domain types trough Spring MVC
    json all --deepSerialize
    web mvc json setup
    web mvc json all
    
    web mvc setup
    web mvc all --package ~.web
    
    // Example scripts for JSON remoting:
    // curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{name: "Thin Crust"}' http://localhost:8080/pizzashop/bases
    // curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '[{name: "Cheesy Crust"},{name: "Thick Crust"}]' http://localhost:8080/pizzashop/bases/jsonArray
    // curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '[{name: "Fresh Tomato"},{name: "Prawns"},{name: "Mozarella"},{name: "Bogus"}]' http://localhost:8080/pizzashop/toppings/jsonArray
    // curl -i -X DELETE -H "Accept: application/json" http://localhost:8080/pizzashop/toppings/7
    // curl -i -X PUT -H "Content-Type: application/json" -H "Accept: application/json" -d '{id:6,name:"Mozzarella",version:1}' http://localhost:8080/pizzashop/toppings
    // curl -i -H "Accept: application/json" http://localhost:8080/pizzashop/toppings
    // curl -i -H "Accept: application/json" http://localhost:8080/pizzashop/toppings/6
    // curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{name:"Napolitana",price:7.5,base:{id:1},toppings:[{name: "Anchovy fillets"},{name: "Mozzarella"}]}' http://localhost:8080/pizzashop/pizzas
    // curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{name:"Stefan",total:7.5,address:"Sydney, AU",deliveryDate:1314595427866,id:{shopCountry:"AU",shopCity:"Sydney",shopName:"Pizza Pan 1"},pizzas:[{id:8,version:1}]}' http://localhost:8080/pizzashop/pizzaorders

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •