如何在hibernate枚举中对多对多映射进行标准限制

时间:2017-07-20 10:02:06

标签: java hibernate enums

枚举类

using System;

namespace LearningCSharp
{
    class MainClass
    {
    public static void Main(string[] args)
    {
        //math
        bool correct = true;
        bool incorrect = false;
        int q1;
        int q2;
        int q3;
        double q4;
        int q5;
        int q6;
        int q7;
        int q8;
        int q9;
        int q10;
        int q11;
        int q12;
        int q13;
        int q14;
        int q15;
        int restart;
        int mainMenuDivisionTest;

        //random
        Random rand = new Random(); //spawning instance of a class
        int randomOne = rand.Next(1,11); //max value is 10 non inclusive
        int randomeTwo = rand.Next(1,11);

        //main menu
        int chooseArea;
        int divisionTest;
        int divisionCalculator;
        int additionCalculator;
        int subtractionCalculator;
        int multiplicationCalculator;

        //Calculators
        int divisionCalculatorA1;
        int divisionCalculatorA2;
        int additionCalculatorA1;
        int additionCalculatorA2;
        int subtractionCalculatorA1;
        int subtractionCalculatorA2;

        //Checker
        int checker;

        MainMenu:
        Console.WriteLine("Press 0 then type enter for a division test.\nPress 1 then type enter for a division calculator.\nPress 2 then type enter for a addition calculator.\nPress 3 then type enter for a subtraction calculator\nPress 4 then type enter for a multiplication calculator");
        chooseArea = Convert.ToInt32(Console.ReadLine());

        if (chooseArea == 0) {
            goto Start;
            BadAnswer:
            Console.WriteLine("You're answer is " + incorrect + ", better luck next time!");
            Console.Write("Press 1 then enter to restart.\nPress 0 then enter to shutdown.\nPress 2 then enter to enter main menu.\nNumber: ");
            restart = Convert.ToInt32(Console.ReadLine());
            if (restart == 1)
            {
                goto Start;
            }
            else if (restart == 0)
            {
                System.Environment.Exit(1);
            }
            else if (restart == 2)
            {
                goto MainMenu;
            }
            else
            {
                System.Environment.Exit(1);
            }

            Start:
            Console.WriteLine("Please do not enter blank spaces, enter letters, and answers with spaces. \nOtherwise the program will crash.");
            Console.Write("What is 10 divided by 2?  ");
            q1 = Convert.ToInt32(Console.ReadLine());
            if (q1 == 5)
            {
                Console.WriteLine(correct);
                Console.WriteLine("Q1 Complete: Good Job, lets move to the next question.");
                Console.WriteLine("Press any key to continue to the next question.\n");
                Console.ReadLine();

                Console.Write("What is 50 divided by 25?  ");
                q2 = Convert.ToInt32(Console.ReadLine());
                if (q2 == 2)
                {
                    Console.WriteLine(correct);
                    Console.WriteLine("Q2 Complete: Good Job, lets move to the next question.");
                    Console.WriteLine("Press any key to continue to the next question.\n");
                    Console.ReadLine();

                    Console.Write("What is 100 divided by 10?  ");
                    q3 = Convert.ToInt32(Console.ReadLine());
                    if (q3 == 10)
                    {
                        Console.WriteLine(correct);
                        Console.WriteLine("Q3 Complete: Good Job, lets move to the next question.");
                        Console.WriteLine("Press any key to continue to the next question.\n");
                        Console.ReadLine();

                        Console.Write("What is 5 divided by 2?  ");
                        q4 = Convert.ToDouble(Console.ReadLine());
                        if (q4 == 2.5)
                        {
                            Console.WriteLine(correct);
                            Console.WriteLine("Q4 Complete: Good Job, lets move to the next question.");
                            Console.WriteLine("Press any key to continue to the next question.\n");
                            Console.ReadLine();

                            Console.Write("What is 600 divided by 2?  ");
                            q5 = Convert.ToInt32(Console.ReadLine());
                            if (q5 == 300)
                            {
                                Console.WriteLine(correct);
                                Console.WriteLine("Q5 Complete: Good Job, lets move to the next question.");
                                Console.WriteLine("Press any key to continue to the next question.\n");
                                Console.ReadLine();

                                Console.Write("What is 5,000 divided by 5?  ");
                                q6 = Convert.ToInt32(Console.ReadLine());
                                if (q6 == 1000)
                                {
                                    Console.WriteLine(correct);
                                    Console.WriteLine("Q6 Complete: Good Job, lets move to the next question.");
                                    Console.WriteLine("Press any key to continue to the next question.\n");
                                    Console.ReadLine();

                                    Console.Write("What is 4,000 divided by 8?  ");
                                    q7 = Convert.ToInt32(Console.ReadLine());
                                    if (q7 == 500)
                                    {
                                        Console.WriteLine(correct);
                                        Console.WriteLine("Q7 Complete: Good Job, lets move to the next question.");
                                        Console.WriteLine("Press any key to continue to the next question.\n");
                                        Console.ReadLine();

                                        Console.Write("What is 1,600 divided by 4?  ");
                                        q8 = Convert.ToInt32(Console.ReadLine());
                                        if (q8 == 400)
                                        {
                                            Console.WriteLine(correct);
                                            Console.WriteLine("Q8 Complete: Good Job, lets move to the next question.");
                                            Console.WriteLine("Press any key to continue to the next question.\n");
                                            Console.ReadLine();

                                            Console.Write("What is 200 divided by 4?  ");
                                            q9 = Convert.ToInt32(Console.ReadLine());
                                            if (q9 == 50)
                                            {
                                                Console.WriteLine(correct);
                                                Console.WriteLine("Q9 Complete: Good Job, lets move to the next question.");
                                                Console.WriteLine("Press any key to continue to the next question.\n");
                                                Console.ReadLine();

                                                Console.Write("What is 6,000 divided by 20?  ");
                                                q10 = Convert.ToInt32(Console.ReadLine());
                                                if (q10 == 300)
                                                {
                                                    Console.WriteLine(correct);
                                                    Console.WriteLine("Q10 Complete: Good Job, lets move to the next question.");
                                                    Console.WriteLine("Press any key to continue to the next question.\n");
                                                    Console.ReadLine();

                                                    Console.Write("What is 60 divided by 300?  ");
                                                    q11 = Convert.ToInt32(Console.ReadLine());
                                                    if (q11 == 0.2)
                                                    {
                                                        Console.WriteLine(correct);
                                                        Console.WriteLine("Q11 Complete: Good Job, lets move to the next question.");
                                                        Console.WriteLine("Press any key to continue to the next question.\n");
                                                        Console.ReadLine();

                                                        Console.Write("What is 1,000 divided by 100?  ");
                                                        q12 = Convert.ToInt32(Console.ReadLine());
                                                        if (q12 == 10)
                                                        {
                                                            Console.WriteLine(correct);
                                                            Console.WriteLine("Q12 Complete: Good Job, lets move to the next question.");
                                                            Console.WriteLine("Press any key to continue to the next question.\n");
                                                            Console.ReadLine();

                                                            Console.Write("What is 100,000 divided by 50,000?  ");
                                                            q13 = Convert.ToInt32(Console.ReadLine());
                                                            if (q13 == 2)
                                                            {
                                                                Console.WriteLine(correct);
                                                                Console.WriteLine("Q13 Complete: Good Job, lets move to the next question.");
                                                                Console.WriteLine("Press any key to continue to the next question.\n");
                                                                Console.ReadLine();

                                                                Console.Write("What is 1,000,000 divided by 50,000?  ");
                                                                q14 = Convert.ToInt32(Console.ReadLine());
                                                                if (q14 == 20)
                                                                {
                                                                    Console.WriteLine(correct);
                                                                    Console.WriteLine("Q14 Complete: Good Job, lets move to the next question.");
                                                                    Console.WriteLine("Press any key to continue to the next question.\n");
                                                                    Console.ReadLine();

                                                                    Console.Write("What is 20,000 divided by 320?  ");
                                                                    q15 = Convert.ToInt32(Console.ReadLine());
                                                                    if (q15 == 62)
                                                                    {
                                                                        Console.WriteLine(correct);
                                                                        Console.WriteLine("Q15 Complete: Good Job, you have finished everything! Score 15/15");
                                                                        Console.Write("Press 1 then enter to restart.\nPress 0 then enter to shutdown.\nPress 2 then enter to enter main menu.\nNumber: ");
                                                                        restart = Convert.ToInt32(Console.ReadLine());
                                                                        if (restart == 1)
                                                                        {
                                                                            goto Start;
                                                                        }
                                                                        else if (restart == 0)
                                                                        {
                                                                            System.Environment.Exit(1);
                                                                        }
                                                                        else if (restart == 2)
                                                                        {
                                                                            goto MainMenu;
                                                                        }
                                                                        else
                                                                        {
                                                                            System.Environment.Exit(1);
                                                                        }
                                                                    }

                                                                    else
                                                                    {
                                                                        Console.WriteLine("Score: 14/15");
                                                                        goto BadAnswer;
                                                                    }
                                                                }

                                                                else
                                                                {
                                                                    Console.WriteLine("Score: 13/15");
                                                                    goto BadAnswer;
                                                                }
                                                            }

                                                            else
                                                            {
                                                                Console.WriteLine("Score: 12/15");
                                                                goto BadAnswer;
                                                            }
                                                        }

                                                        else
                                                        {
                                                            Console.WriteLine("Score: 11/15");
                                                            goto BadAnswer;
                                                        }
                                                    }

                                                    else
                                                    {
                                                        Console.WriteLine("Score: 10/15");
                                                        goto BadAnswer;
                                                    }
                                                }

                                                else
                                                {
                                                    Console.WriteLine("Score: 9/15");
                                                    goto BadAnswer;
                                                }
                                            }

                                            else
                                            {
                                                Console.WriteLine("Score: 8/15");
                                                goto BadAnswer;
                                            }
                                        }

                                        else
                                        {
                                            Console.WriteLine("Score: 7/15");
                                            goto BadAnswer;
                                        }
                                    }

                                    else
                                    {
                                        Console.WriteLine("Score: 6/15");
                                        goto BadAnswer;
                                    }
                                }

                                else
                                {
                                    Console.WriteLine("Score: 5/15");
                                    goto BadAnswer;
                                }
                            }

                            else
                            {
                                Console.WriteLine("Score: 4/15");
                                goto BadAnswer;
                            }
                        }

                        else
                        {
                            Console.WriteLine("Score: 3/15");
                            goto BadAnswer;
                        }
                    }

                    else
                    {
                        Console.WriteLine("Score: 2/15");
                        goto BadAnswer;
                    }
                }

                else
                {
                    Console.WriteLine("Score: 1/15");
                    goto BadAnswer;
                }
            }

            else
            {
                Console.WriteLine("Score: 0/15");
                goto BadAnswer;

            }

                if (chooseArea == 1) {
                Console.WriteLine("Press Enter to Continue to Division Calculator");
                Console.ReadKey();
                DivisionStart:
                Console.WriteLine("Write Number (1): ");
                divisionCalculatorA1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Write Number (2): ");
                divisionCalculatorA2 = Convert.ToInt32(Console.ReadLine());

                ChooseNumberD:
                Console.WriteLine("Is you're question " + divisionCalculatorA1 + "divided by " + divisionCalculatorA2 + "?\n0 for No\n 1 for Yes");
                checker = Convert.ToInt32(Console.ReadLine());
                if (checker == 0)
                {
                    goto DivisionStart;
                }
                else if (checker == 1)
                {
                    Console.WriteLine("The answer is: " + divisionCalculatorA1 / divisionCalculatorA2);
                    Console.WriteLine("Would you like to do another division question?\n0 for main menu\n 1 for Yes\n 2 for Close Application");
                    checker = Convert.ToInt32(Console.ReadLine());
                    if (checker == 0)
                    {
                        goto MainMenu;
                    }
                    else if (checker == 1)
                    {
                        goto DivisionStart;
                    }
                    else if (checker == 2)
                    {
                        System.Environment.Exit(1);
                    }
                }
                else
                {
                    Console.WriteLine("Please choose a number.");
                    goto ChooseNumberD;
                }
            }
        }
    }
  }
}

模型类

public enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,SATURDAY }

hibernate查询出错:

@Fetch(FetchMode.SUBSELECT)
@ElementCollection(targetClass = Days.class, fetch = FetchType.EAGER)
@JoinTable(name = "bus_days", joinColumns = @JoinColumn(name = "bus_master_id"))
@Column(name = "day", nullable = false)
@Enumerated(EnumType.ORDINAL)
private Set<Days> days = new HashSet<Days>();
// getter setter

在eclipse控制台中获得的错误;

Criteria criteria = null;
 criteria = session.createCriteria(Model.class)
          .add(Restrictions.eq("fromCity.id", fromCity))
          .add(Restrictions.eq("toCity.id", toCity))
          .add(Restrictions.eq("days", days.ordinal()));//error in this line
            return (List<Model>) criteria.list();

帮帮我,

1 个答案:

答案 0 :(得分:0)

您不应该按days.ordinal()进行比较,只需按days进行比较,hibernate会自行将其转换为数字。